riesenia/cakephp-routing

CakePHP plugin for routing by attributes

Installs: 1 056

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 2

Open Issues: 0

Type:cakephp-plugin

v1.2.0 2024-09-11 08:22 UTC

This package is auto-updated.

Last update: 2024-11-11 08:41:11 UTC


README

The riesenia/cakephp-routing package allows you to define routes using class attributes in your controllers. These routes are then compiled into a routes_compiled.php file, which is included in your application's routes.php file.

Configuration

if you want to add routes for controllers not in the default app namespace APP, pass namespace option to the command

routes:build -n Plugin

Defining routes

  1. Define your class routes using the Resources attribute.
<?php
namespace App\Controller;
use Riesenia\Routing\Attribute\Resources;

#[Resources(only: ['index', 'view'])]
class AuthorsController extends AppController
{
}
  1. Define your method routes using the Connect attribute.

When defining routes using the Connect attribute in PHP, adding / to the URI will stop prefixing your route with the controller name.

<?php
namespace App\Controller;
use Riesenia\Routing\Attribute\Connect;

class AuthorsController extends AppController
{
    #[Connect(uri: 'cool-author')]
    public function index()
    {
        // Controller logic for /authors/cool-author
    }

    #[Connect(uri: '/custom-author')]
    public function custom()
    {
        // Controller logic for /custom-author
    }
}

Resulting Routes:

$builder->connect('/authors/cool-author', 'Authors::index',[]);
$builder->connect('/custom-author', 'Authors::custom',[]);

Compiling Routes

Run the Routes Command:

This will compile your routes into a routes_compiled.php file.

Using Compiled Routes

Update your routes.php to include the compiled routes.

<?php
use Cake\Routing\RouteBuilder;

return static function (RouteBuilder $routes) {
    require CONFIG . 'routes_compiled.php';
};