-
Notifications
You must be signed in to change notification settings - Fork 0
Controllers
If you'd rather use a class to group related route actions together you can pass a Controller String to map()
instead of a closure. The string takes the format {name of class}@{name of method}
. It is important that you use the complete namespace with the class name.
Example:
// HelloWorldController.php
namespace Mvc\App\MyControllers;
class HelloWorldController
{
public function sayHello()
{
return 'Hello World';
}
}
// routes.php
$router->map(['GET'], 'hello-world', 'Mvc\App\MyControllers\HelloWorldController@sayHello');
// or set default namespace
$router->setDefaultNamespace('Mvc\App\MyControllers');
$router->map(['GET'], 'hello-world', 'HelloWorldController@sayHello');
You can create controllers that automatically handle all of the route/CRUD requests. When creating a resource controller, you can implement the resource controller interface: Qubus\Routing\Interfaces\ResourceController
, but you don't have to. You can extend the interface to override some of the methods and their parameters or create your own interface based on the specifications of your project/application.
namespace Qubus\Routing\Interfaces;
interface ResourceController
{
/**
* Display a listing of the resource.
*/
public function index();
/**
* Show the form/view for creating a new resource.
*/
public function create();
/**
* Display the specified resource.
*
* @param int $id
*/
public function show($id);
/**
* Store a newly created resource in storage.
*/
public function store();
/**
* Show the form/view for editing the specified resource.
*
* @param int $id
*/
public function edit($id);
/**
* Update the specified resource in storage.
*
* @param int $id
*/
public function update($id);
/**
* Remove the specified resource from storage.
*
* @param int $id
*/
public function destroy($id);
}
If you want to use middleware in your resource controller, then your controller should extend the abstract controller class: Qubus\Routing\Controller\BaseController
.
use Qubus\Routing\Controller\BaseController;
use Qubus\Routing\Interfaces\ResourceController;
use Middleware\AddHeaderMiddleware;
class PostController extends BaseController implements ResourceController
{
public function __construct()
{
$this->middleware(new AddHeaderMiddleware('X-Key1', 'abc');
}
public function index()
{
return 'Index of post controller.';
}
```
}
Register a resourceful route to the controller:
$router->resource('posts', 'PostController');
To register multiple resource controllers by passing in an array in the resource
method:
$router->resources([
'posts' => 'PostController',
'users' => 'UserController'
]);
Actions Handled By Resource Controller
Verb | Uri | Action | Route Name |
---|---|---|---|
GET | /posts | index | posts.index |
GET | /posts/create | create | posts.create |
POST | /posts/store | store | posts.store |
GET | /posts/{posts} | show | posts.show |
GET | /posts/{posts}/edit | edit | posts.edit |
PUT/PATCH | /posts/{posts} | update | posts.update |
DELETE | /posts/{posts} | destroy | posts.destroy |
You can conveniently create controllers that will be consumed by an api by using the apiResource
method:
$router->apiResource('users', 'UserRestController');
You can register several api resources by passing in an array to the apiResources
method:
$router->apiResources([
'posts' => 'PostRestController',
'users' => 'UserRestController'
]);