i know that Router's prefix method adds a prefix to the routes but am still confused what the scope method does to routes.is it just an alias for prefix or has its own use.
Router::prefix('api', function ($routes) {
$routes->scope('/v1', function ($routes) {
$routes->connect('/', ['action'=>'index']);
$routes->connect('/:id', ['action'=>'view', ':id']);
});
});
Both allow sharing of common path segments. The difference is that a prefix will look for a controller within a sub-namespace.
from the documentation:
Prefixes are mapped to sub-namespaces in your application’s Controller namespace ... Using our users example, accessing the URL
/admin/users/edit/5
would call theedit()
method of oursrc/Controller/Admin/UsersController.php
passing 5 as the first parameter. The view file used would besrc/Template/Admin/Users/edit.ctp
In the above case, a scope would look for the controller at src/Controller/UsersController.php
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With