Reputation: 65
In my routes.php file, I've put this into Router::scope('/', function ($routes) {
$routes->connect( '/:controller/:id', ['action' => 'view'], ['id' => '[0-9]+'] );
However it does not seem to work? The URLs still go to players/view/1
I'm not sure what I'm doing wrong
Upvotes: 2
Views: 631
Reputation: 962
From the book: book.cakephp.org/3.0/en/development/routing.html#route-elements (just below the code you may have cut-and-pasted):
CakePHP does not automatically produce lowercased and dashed URLs when using the :controller parameter. If you need this, the above example could be rewritten like so:
$routes->connect(
'/:controller/:id',
['action' => 'view'],
['id' => '[0-9]+', 'routeClass' => 'DashedRoute']
);
...and then a little further down:
Once this route has been defined, requesting
/apples/5
would call theview()
method of the ApplesController. Inside theview()
method, you would need to access the passed ID at$this->request->params['id']
.
So in my (for example) ApplesController's view()
method, I added the following at the very top:
if ($id == null) {
$id = $this->request->params['id'];
}
...and now apples/view/5
and apples/5
both work the same way.
Upvotes: 0