Skip to content

Controllers

Aouf Ali edited this page Dec 19, 2018 · 1 revision

Controllers

Introduction

Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the /app/Controllers/ directory and should have the name nameController.php.

Basic controller

here is a basic controller class:

<?php  
  
use Luna\Core\Controller;  
use Luna\Services\Http\{Request, Response};  
  
class UsersController extends Controller  
{  
  
  public function __invoke()  
	 {  
	 echo "hello world!";  
	  }  
  
  public function index($data,Request $request,Response $response)  
	 { 
	 echo "hello coders!";
	  }  

}

Define & use a controller

You can define a route to this controller action like so:

Route::get('user/{id}', 'User@index');
# That's it! no other registration no other including! just this!

notice: the url dynamic parts will be passed to your function in the array parameter data

Clone this wiki locally