Cakephp 3 remove slug on homepage

I have a slug called “Home”, and I want to use that as homepage.

So for example:

localhost:8000/home

Should become:

localhost:8000

How can I do this?
Already tried to set the slug as / but that didn’t solve it.

Cheers,
Stephan

Hi Udart,
You have to do something like this:

$routes->connect(’/’, [‘controller’ => ‘Pages’, ‘action’ => ‘display’, ‘home’]);

In your config/routes.php file. With that line, you are telling the framework to connect to some specific URL (action + controller) in the specified case (localhost:8000).

You can read more about routing here:

https://book.cakephp.org/3.0/en/development/routing.html#routes-configuration

Saludos,

Thanks for your reply.

By default I already had the routing set as that.

But as I am using slugs, because I am creating dynamic page/content. It works for other pages, where I can see the content, but only the home page doesn’t show the dynamic content.

I would like to be able to set any page as home.

Seems I already found the solution.

I changed the routing to just the following line:

$routes->connect('/*', ['controller' => 'pages', 'action' => 'view']);

Then I changed the view as followed:

public function view($slug = null) 
{
    $pages = TableRegistry::getTableLocator()->get('webpages');
    if($slug == null){
        $query = $pages->find('all', [
            'conditions' => ['ishome' => 1]
        ]);
    } else {
        $query = $pages->find('all', [
            'conditions' => ['slug' => $slug]
        ]);
    }
    $page = $query->first();
    $this->set(compact('page'));
}

I use the answer from the following comment, but had to modify it a bit, since that code was used for an older version of cakephp (I am using cakekphp 3.8).

1 Like