PagesController & Routes CakePhP 4

Want to develop a new homepage and visit it by www.abc.com/the-new-homepage. So, I want www.abc.com to redirect to www.abc.com/the-new-homepage.

I’m not getting the expected result yet, I’m trying get things clear on my testbed, tried several options, but I can’t get it right. It looks a bit like https://discourse.cakephp.org/t/pagescontroller-ignores-pages-dir/5087 to me?

The homepage shows up when not routed:

2021-10-01-14-26-58_scrot

2021-10-01-14-20-28_scrot

The only thing what wonders here is I left out the ‘display’, but the page shows up anyway, expected something like a missing method.

    <?= $this->Html->link(__('homepage'), ['controller' => 'pages', 'action' => 'myhomepage']) ?>

Setting up the routes for groups and individuals don’t give any problems as expected, the route to the page does.

$routes->connect('/the-new-homepage',['controller' => 'Pages', 'action' => 'myhomepage']);

$routes->connect('/all-groups', ['controller' => 'Groups', 'action' => 'index']);

$routes->connect('/all-individuals', ['controller' => 'Individuals', 'action' => 'index']);

The route looks like I want to have it:

but it now I get an unexpected missing method:

Thought maybe getting the ’ display’ into place would help, but it didn’t:

    <?= $this->Html->link(__('homepage'), ['controller' => 'pages', 'action' => 'display',  'myhomepage']) ?>


$routes->connect('/the-new-homepage',['controller' => 'Pages', 'action' => 'display', 'myhomepage']);

Route isn’t ‘active’, homepage shows up but with /pages/myhomepage adress.

Hope someone get things clear to me.

Well there are some issues I can see and will try to make clear:

First of all to clearify what $routes->connect() does:
$routes->connect() create a connection between a URL and what it should display/render on that given URL.
$routes->connect() does NOT redirect from one URL to another.

So

$routes->connect('/the-new-homepage',['controller' => 'Pages', 'action' => 'display', 'myhomepage']);

will show you what is present in templates/Pages/myhomepage.php because that is what the src/Controller/PagesController.php function display() is meant to do.

If you want to redirect via the CakePHP Router you will have to use
https://book.cakephp.org/4/en/development/routing.html#redirect-routing

Second of all be aware of upper and lowercase letters. Controllers are CamelCased.
See CakePHP Conventions - 4.x

So maybe

<?= $this->Html->link(__('homepage'), ['controller' => 'pages', 'action' => 'display',  'myhomepage']) ?>

causes some problems because it should actually be

<?= $this->Html->link(__('homepage'), ['controller' => 'Pages', 'action' => 'display',  'myhomepage']) ?>

Thanks for reply, still no success, but you’re answer raises new questions. I’m trying to get things working in 3 steps:

  1. Get the page working without routing: It behaves like I want it to behave, but the question is, why does it work :grinning:

  2. Get the page working with routing: Doesn’t work, maybe because of a mistake in step 1, maybe wrong code, no idea yet.

  3. Redirecting, because step 2 not completed, but you’re answer is food for thought.

In step 1 the question remains why

    <?= $this->Html->link(__('homepage'), ['controller' => 'pages', 'action' => 'myhomepage']) ?>

does behave like

    <?= $this->Html->link(__('homepage'), ['controller' => 'Pages', 'action' => 'display', 'myhomepage']) ?>

In step 1, despite not using capital and ‘display’, it doesn’t seem to make any difference, but should it, or what are the consequences?

In step 2 the route

$routes->connect('/the-new-homepage',['controller' => 'Pages', 'action' => 'display', 'myhomepage']);

with the link

    <?= $this->Html->link(__('homepage'), ['controller' => 'Pages', 'action' => 'display', 'myhomepage']) ?>

gives no error, but the route doesn’t work, url stays ‘…/pages/myhomepage’.

The route

$routes->connect('/the-new-homepage',['controller' => 'Pages', 'action' => 'myhomepage']);

with the link

    <?= $this->Html->link(__('homepage'), ['controller' => 'Pages', 'action' => 'myhomepage']) ?>

gives the shown error, although the route is ok, ‘…/the-new-homepage’ (So it looks like it does matter to use or not use ‘display’ in step 1)

Tried all the 4 combo’s, route and link with or without ‘display’ just to see what happens. the route with both ‘display’ and ‘myhomepage’ always gives the error.

So, probably making an syntax error in the route (Although the wanted route is showing up)?

Step 3, not up to yet, but I think I want to connect the url ‘/’ to ‘the-new-homepage’, so a route should work? Guess when using redirect, I’m never able to reach ‘homepage’ anymore? But that’s of later concern.

Googling around to find some working examples, I stumbled across the routes.php of cakephp.org (github)

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

So, I’m using the right syntax, but, I think it’s not working as intended on cakephp.org either: