CakePHP - Html-> link - why use controller => and action => instead of controller or action

Why is this:

echo $this->Html->link('Add a User', array('controller'=>'users', 'action'=>'add')); 

Instead of this:

 echo $this->Html->link('Add a User', 'users/add'); 
+4
source share
2 answers

The second example will always generate the URL 'users / add'. The first method makes it possible to use reverse routing to generate an individual URL, as defined by the rules in the routes.php file.

In practice, I often find that there is no difference between the first and second styles. However, if you later decide to make changes to your routes, you may find that doing things for the first time saves time in the long run, so you don't need to go back and change the path for each link ...

+8
source

For future reference ... use the first option because the second is relative to location. For example, if you are at www.example.com/post, the final URL would be:

for # 1 www.example.com/post/users/add for # 2 www.example.com/users/add

Sorry for the bad English: P

0
source

Source: https://habr.com/ru/post/1345937/


All Articles