2

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

1 Answer 1

0

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 the view() method of the ApplesController. Inside the view() 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.