DijkeMark DijkeMark - 16 days ago 9
PHP Question

Cakephp 3 - creating link with admin prefix not working

I am trying to create a link in the default.ctp layout file that links to an admin page.

In route.php I have this:

Router::prefix('admin', function ($routes) {
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->connect('/login', array('controller' => 'Users', 'action' => 'login'));
$routes->connect('/logout', array('controller' => 'Users', 'action' => 'logout'));

$routes->fallbacks(DashedRoute::class);
});


In default.ctp template file I have tried this:

echo $this->Html->link('Build Settings', '/buildsettings', array('admin' => true));
echo $this->Html->link('Build Settings', '/buildsettings', array('prefix' => 'admin'));
echo $this->Html->link('Build Settings', '/buildsettings', array('prefix' => 'admin', 'admin' => true));


However, the link it creates is this:

<a href="/buildsettings" admin="1">Build Settings</a>


While it should make something like this:

<a href="/admin/buildsettings">Build Settings</a>


Going to /admin/buildsettings, actually goes to the admin buildsettings controller, so I know the routing itself works, just not creating the proper links.

What am I doing wrong here?

Answer

What you're looking for is a little hidden in the documentation. In fact, the direct thing you're looking for, I believe, doesn't exist at all. You can add prefixes when you link to a controller and action, but for giving a link without a controller you can not add a prefix. However, there is a work around to achieve what you want.

'prefix' => 'admin'

Used such as

<?php echo $this->Html->link('Build Settings', ['prefix' => 'admin','controller' => 'buildsettings']); ?>
Comments