Skip to content

Controllers

Joshua Parker edited this page Sep 1, 2020 · 9 revisions

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 \MyControllers;

class HelloWorldController
{
    public function sayHello()
    {
        return 'Hello World';
    }
}

// routes.php
$router->map(['GET'], 'hello-world', '\MyControllers\HelloWorldController@sayHello');
// or set default namespace
$router->setDefaultNamespace('\MyControllers');
$router->map(['GET'], 'hello-world', 'HelloWorldController@sayHello');

Resource Controller

You can create controllers that automatically handle all of the route/CRUD requests. When creating a resource controller, it should implement the resource controller interface: Qubus\Router\Interfaces\ResourceControllerInterface.

namespace Qubus\Router\Interfaces;

interface ResourceControllerInterface
{
    /**
     * 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\Router\Controller.

use Qubus\Router\Controller;
use Qubus\Router\Interfaces\ResourceControllerInterface;
use Middleware\AddHeaderMiddleware;

class PostController extends Controller implements ResourceControllerInterface
{
    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
Clone this wiki locally