Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maymeow/cake-attribute-routing
🌐 Attribute routing for CakePHP 4.*
https://github.com/maymeow/cake-attribute-routing
attributes cakephp cakephp-plugin cakephp4 php8 routing
Last synced: 19 days ago
JSON representation
🌐 Attribute routing for CakePHP 4.*
- Host: GitHub
- URL: https://github.com/maymeow/cake-attribute-routing
- Owner: MayMeow
- License: mit
- Created: 2021-11-07T11:21:43.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-09T20:49:04.000Z (about 3 years ago)
- Last Synced: 2024-11-18T09:49:27.949Z (about 1 month ago)
- Topics: attributes, cakephp, cakephp-plugin, cakephp4, php8, routing
- Language: PHP
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Attribute Routing plugin for CakePHP
Attribute routing for CakePHP 4.*
## Requirements
- CakePHP 4.x
- PHP 8.x :warning: This plugin using features that are available in PHP 8 and later.
- Cache (tested with redis), all routes are cached before can be used.## Installation
You can install this plugin into your CakePHP application using [composer](https://getcomposer.org).
The recommended way to install composer packages is:
```
composer require maymeow/cake-attribute-routing
```And load it.
```bash
php bin/cake.php plugin load MayMeow/Routing
```## Configuring
Add new key `Controllers` somewhere in application configuration and provide
controller names where you want to use Attribute Routes.```php
// app_local.php
'Controllers' => [
\App\Controller\UsersController::class
]
```## Usage
To define route add attribute `Route` with required path to the methods as follows:
```php
#[Route('/users')]
public function index()
{
$users = $this->paginate($this->Users);$this->set(compact('users'));
}
```
### Wildcard parametersSometimes you need to pass parameters to your action in that case you can use wildcard route:
```php
#[Route('/users/edit/*')]
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => [],
]);
// ...
}
```:warning: If you're creating wildcard route you must define all other routers in sub-path.
### Named parameters
Instead of using wildcard you can use named parameters. This is usable wne you want to pass more than one
parameter to your controllers' action.```php
#[Route('/users/:id/details', options: ['id' => '\d+', 'pass' => ['id']])]
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => [],
]);$this->set(compact('user'));
}
```When you're creating route with named parameter you have to pass options to tell router which parameters
you want to pass to controller.Path to controller action `Controller::action` is generated automatically from
controller class name and method name;## Troubleshooting
This plugin using cache for routes: all routes need to be cached before use. If you have problem with
routes try to clear cache.Fond bug? Or do you want new feature? Open new Issue https://github.com/MayMeow/cake-attribute-routing/issues
License MIT