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:

Taking a better look, it seems obvious what’s happening.

The Pages-controller only has one function, display.

When showing pages, the url isn’t ‘…/pages/display/name_of_page’ but ‘…/pages/name_of_page’.

Removing the ‘$builder->connect(’/pages/*’, ‘Pages::display’);’ I’m able to use the intended route without a problem:

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

Its just a fallback that URLs are generated like /controller/action/param1/param2
Thats what this line does. app/routes.php at 4.x · cakephp/app · GitHub
Just like the comments say.

If you define some route before that line to a specific controller action then that Route will be used to generate links.
Also you can check the Routes Tab of the DebugKit to see which Routes (and in what order) these are present.

Sorry, I’m afraid I completely lost you. Can’t relate this to what I’ve done or to my question? Maybe you could give some extra explanation?

Alright, let me be even more clearer.

You can define multiple URLs to show the same controller action.

    $builder->connect('/other-home', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $builder->connect('/another-home', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

All 3 URLs show the same content.

But when you generate a URL via e.g. this code

  echo $this->Html->link('Test', [
    'controller' => 'Pages',
    'action' => 'display',
    'home'
  ]);

It will use the FIRST match it gets from your routes, so /other-home

Yeah, ok, but related to my issue, what about that fallback you come up with:

The line you’re linking to:

        $builder->fallbacks();

That’s where i get lost.

Let me copy the comments from that file

        /*
         * Connect catchall routes for all controllers.
         *
         * The `fallbacks` method is a shortcut for
         *
         * ```
         * $builder->connect('/{controller}', ['action' => 'index']);
         * $builder->connect('/{controller}/{action}/*', []);
         * ```
         *
         * You can remove these routes once you've connected the
         * routes you want in your application.
         */
         $builder->fallbacks();

If you remove that line all routes built like /<controller>/<action> will stop working and only those you define yourself will work.

Well, I’m aware of the comment, you’re trying to point to something, just can’t figure out why I should remove that line.

I’m not sure if I have now connected all routes I want, or in the future, so I don’t want to remove that line.

The comment says ‘can’, not ‘have to’. So I expect I can leave it in place without any consequences, don’t experience any misbehavior.

Don’t know if I want that to happen, maybe you recommend removing the line for testing purposes? Just want to leave that line in place, I simply don’t get why it should help me with my issue.

By any chance, did you misread?

I have never removed $builder->fallbacks(); but:

So, I want www.abc.com to redirect to www.abc.com/the-new-homepage.

So did you set a redirect route as explained here? Routing - 4.x

I haven’t tried to redirect the route until yesterday. Tried to implement something like you pointed to. Don’t know what I’m actually doing, trying to get there by trial-and-error to get the idea.

At the moment when using the url ‘localhost:8765’ there seems to be something recursing/looping to some point, I’m stuck with:

Although I want to understand what’s happening, what bothers me the most is that I replaced the routes.php with the originally baked one, I cleared all caches, and still getting the error.

This is how my routes look like, I don’t know how the pages-routes have to look like:

Cleared all Cakephp-caches, not the browser cache. When browser-cache was cleared, the baked route.php worked like expected. Guess at some point I used the ‘persist => true’ in the mentioned example, and this did what it had to do …

Thanks for taking care of this (GitHub).

The now used routes.php is clear to me now. The ‘$builder’-parts aren’t used anymore, when looking at the routes-documentation, didn’t get clear why (not) to use these. Apparently can do without them.

Now I did, works fine.