Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp difference between Router::scope and Router::prefix

Tags:

cakephp-3.0

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']);         
}); 
});  
like image 672
Cholthi Paul Ttiopic Avatar asked Sep 28 '22 00:09

Cholthi Paul Ttiopic


1 Answers

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 the edit() method of our src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be src/Template/Admin/Users/edit.ctp

In the above case, a scope would look for the controller at src/Controller/UsersController.php.

like image 77
Elfalem Avatar answered Oct 06 '22 20:10

Elfalem