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

Wednesday, March 9, 2022

[FIXED] Cakephp 3 - creating link with admin prefix not working

 March 09, 2022     cakephp, cakephp-3.0, php, routing     No comments   

Issue

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?


Solution

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']); ?>


Answered By - user5236938
  • 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