-
-
Notifications
You must be signed in to change notification settings - Fork 46
5. Controllers
You can use Controller Class in PHP-Router. Let's make a demo with controller:
# index.php file
$router = new \Buki\Router\Router([
'paths' => [
'controllers' => 'Controllers',
],
'namespaces' => [
'controllers' => 'Controllers',
],
]);
$router->get('/', 'IndexController@main');
# OR
# $router->get('/', ['IndexController', 'main']);
# OR
# $router->get('/', [IndexController::class, 'main']);
// other examples...
$router->get('/create', 'IndexController@create');
$router->post('/store', 'IndexController@store');
$router->get('/edit/:id', 'IndexController@edit');
$router->put('/update/:id', 'IndexController@update');
$router->delete('/delete/:id', 'IndexController@delete');
We've just generated some REST endpoints as example.
Now, for this example; We should have a controller file that called IndexController.php
in Controllers
directory. Because we configured controller path as Controllers
. So, we have following directory structure:
- Controllers
--- IndexController.php
- index.php
If there are some else directory in Controllers
directory like Backend, Api or something else, you can define routes as following:
$router->get('/create', 'Backend.IndexController@create');
$router->post('/store', 'Backend.IndexController@store');
# OR
$router->get('/create', 'Backend\\IndexController@create');
$router->post('/store', 'Backend\\IndexController@store');
# OR
$router->get('/create', [Backend\IndexController::class, 'create']);
$router->post('/store', [Backend\IndexController::class, 'store']);
Default main method that you will use in controllers is main
. If you want to change this default method name, you can set main_method
value to PHP-Router configs.
You can only add an __invoke
method in your controller and define a route by specifying only the controller like this:
namespace Controllers;
use Buki\Router\Http\Controller;
class FooController extends Controller
{
public function __invoke()
{
return 'Hello from Invoke!';
}
}
$router->get('/foo', 'FooController');
# $router->get('/foo', FooController::class);
Actually, You don't need to define all routes manually. If you want to generate routes using all methods in Controller as automatically, you can use controller()
method in PHP-Router. For example:
# Controllers/IndexController.php
namespace Controllers;
use Buki\Router\Http\Controller;
class IndexController extends Controller
{
public function main()
{
return 'Main method';
}
public function foo()
{
return 'Foo method';
}
public function bar($id)
{
return "Bar method {$id}";
}
public function baz(int $id, $name = 'PHP-Router')
{
return "Baz method {$id} - {$name}";
}
public function hello(int $id, string $name)
{
return "Hello method {$id} - {$name}";
}
}
$router->controller('/', 'IndexController');
# OR
$router->controller('/', IndexController::class);
All routes will be generated together method name and parent path that you defined in controller method and their required and not required parameters. For the example at above, following routes will be generated:
/
/foo
/bar/:any
/baz/:id/:any?
/hello/:id/:slug
Also, you can define method type via method name. It's simple! For example:
use Buki\Router\Http\Controller;
class IndexController extends Controller
{
public function main()
{
return 'Main method';
}
public function getFoo()
{
return 'Foo method';
}
public function postBar()
{
return 'Bar route can only be worked POST method.';
}
}
You can use all HTTP method type in PHP-Router like this while you define a new route method.
Also, If you use method name as camelCase, it will be convert snake-case in Route path automatically. For example:
use Buki\Router\Http\Controller;
class IndexController extends Controller
{
// other methods
public function getHelloWorld()
{
return 'hello-world route';
}
}
# Route path will be like this for the method: /hello-world