riesenia / cakephp-routing
CakePHP plugin for routing by attributes
Installs: 2 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 2
Open Issues: 0
Type:cakephp-plugin
pkg:composer/riesenia/cakephp-routing
Requires
- php: ^8.1
- cakephp/cakephp: ^5.0.1
- kcs/class-finder: ^0.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.1
- rshop/php-cs-fixer-config: ^3.0
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
- Define your class routes using the
Resourcesattribute.
<?php namespace App\Controller; use Riesenia\Routing\Attribute\Resources; #[Resources(only: ['index', 'view'])] class AuthorsController extends AppController { }
- Define your method routes using the
Connectattribute.
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'; };