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

Friday, March 11, 2022

[FIXED] HTML helper doesn't output appropriate link based on parameters

 March 11, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

For some reason, I cannot get HTML helper to output /users/workspace

I'm currently testing the following code on /users/login

echo '<li>'.$this->Html->link(h($this->request->getParam('controller')),['controller' => $this->request->getParam('controller'), 'action' => 'workspace']).'</li>';
echo '<li>'.$this->Html->link(h($this->request->getParam('controller')),['controller' => $this->request->getParam('controller'), 'action' => 'anything-here-works']).'</li>';

Will output:

<li><a href="/slat">Users</a></li>
<li><a href="/slat/users/anything-here-works">Users</a></li>

Is workspace a reserved word or something? What am I missing or not understanding?

I'm on CakePHP 3.10.

Noting a route setup:

$routes->connect('/', ['controller' => 'Users', 'action' => 'workspace']);

Solution

$routes->connect('/', ['controller' => 'Users', 'action' => 'workspace']);

As i understood, the base path routing is already defined in the route as above, so it generates "<li><a href="/slat">Users</a></li>", when the link created for

"$this->Html->link(h($this->request->getParam('controller')),['controller' => $this->request->getParam('controller'), 'action' => 'workspace'])"

here controller = "Users" and action="workspace", hence it gets the base path "/slat" as matches with "/" on the route.

i think "slat" is your project directory on localhost.

You can generate links in more customized way, ie:-

<a href="<?= $this->Url->build(['controller'=>'Users','action'=>'workspace']);">" >Link</a>

And if you need routing for specific users type ( like admin ), you can create prefix routing prefix routing on cakephp 3.x

also, $this->request->getParam('controller') gives the current controller name, so the link might become wrong when you jump to other pages.

you can mention the controller name directly instead of using "getParam"

also for generating link for current controller's action ( which is mentioned on the template file of the current controller only ), you do not need to mention the controller's name, only action name and parameters are sufficient.

ie:

<a href="<?= $this->Url->build(['action'=>'workspace']);">" >Link</a>


Answered By - D Coder
  • 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