PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] CakePHP reverse routing not working

 February 22, 2022     cakephp-3.0, reverse, routing     No comments   

Issue

I've looked around and while there's a number of similar questions to this, an answer has never been given that works:

I'm using CakePhp 3.0. I am attempting to connect and link some basic page urls to the framework.

In terms of normal routing this has worked as expected. For debugging purposes I have stripped out all routes, so the following are the only active routes at the moment:

Router::scope('/', function ($routes) {
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home', '_name' => 'home']);
    $routes->connect('/events', ['controller' => 'Pages', 'action' => 'display', 'events', '_name' => 'events']);
    $routes->fallbacks('DashedRoute');
});

If you manually go to / or /events these work as expected.

Now, if I attempt to reverse route (as is best practice), I use the following:

echo $this->Html->link('News', ['controller' => 'Pages', 'action' => 'home', '_full' => true] );

.. and ..

echo $this->Html->link('Events', ['controller' => 'Pages', 'action' => 'events', '_full' => true] );

These incorrectly give me /pages/home and /pages/events. In fact, / and /events are what is required.

Surely, this is one of the most simple uses of the reverse routing process.. so it must be achievable. However - the documentation isn't at all clear about this. It may be in there, but I have read it quite a few times.

Can anyone explain how to correctly do this?

Thanks

Rick


Solution

The call to connect() is slightly incorrect. The _name parameter should be in the third argument of the function call:

$routes->connect('/events', ['controller' => 'Pages', 'action' => 'display', 'events'], ['_name' => 'events']);

So you can just use the route name in your template:

$this->Html->link('Events', ['_name' => 'events']);


Answered By - José Lorenzo Rodríguez
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing