2

In CakePHP 2.x there is a HTML Helper which allows for creating hyperlinks within Views.

If I use this...

<?php
    echo $this->Html->link('Navigation', 
    array('controller' => 'navigations', 'action' => 'foo')
    );
?>

... it will generate a URL to /navigations/foo (NavigationsController::foo).

However if I use index as the action

<?php
    echo $this->Html->link('Navigation', 
    array('controller' => 'navigations', 'action' => 'index')
    );
?>

The URL becomes /navigations.

I understand about "index" being a default when it comes to webpages. However, I actually need the URL to be /navigations/index, or at the very least have a trailing slash (/navigations/).

Is this possible?

5
  • This is a routing problem. You need to remove the route that defines index as a default action. So that routing only matches when an action is defined. I would post an answer, but I don't remember Cake 2 routing very well. Commented Jan 16, 2018 at 14:28
  • 1
    Do you really need it to be like that? From my experience, most of the time people think they need something like that, they're just confused about something else. Commented Jan 16, 2018 at 14:32
  • It depends what you mean by need. Other parts of the site have loads of ajax calls which have been written like this: $.get('get_data'). This will result in a 404 if you're accessing the page as /navigations but if it contains a trailing slash /navigations/ it will work fine. The reason for this is because the ajax request should be made to, for example, https://localhost/navigations/get_data whereas without the trailing slash it will request https://localhost/get_data (404 error as that doesn't exist). The alternative is to replace all the URL's in the ajax requests. Commented Jan 16, 2018 at 14:35
  • @ndm you were right. lol Commented Jan 16, 2018 at 14:55
  • @andy $.get('get_data') is a code smell. You shouldn't write code that makes assumptions about the URL state. Commented Jan 16, 2018 at 14:58

1 Answer 1

2

Fix the AJAX URLs instead

With regards to the explanation in your comment, I would argue that the "correct" way of fixing this problem is to use proper root relative URLs for the AJAX calls, as changing the URL structure only cloakes the underlying problem, that is targeting non-unique resources.

Personally I alwas use Router::url() to generate the URLs, either in the specific script files when serving them via PHP, or by writing out configuration in for example the layout, being it globally so that the scripts can access it when needed:

<script>
var config = {
    navigationDataUrl: <?php echo json_encode(
        Router::url(array('controller' => 'Navigations', 'action' => 'get_data'))
    ) ?>
};
</script>

or by configuring possibly existing JS objects.

<script>
navigation.dataUrl = <?php echo json_encode(
    Router::url(array('controller' => 'Navigations', 'action' => 'get_data'))
) ?>;
</script>

Connect routes with an explicit index part

That being said, for the sake of completion, it is possible to force the index part to not be dropped, by connecting routes that explicitly define that part, as opposed to using the :action route element, like:

Router::connect('/:controller/index', array('action' => 'index'));

which would match/catch URL arrays like yours before they are being handled by CakePHPs default controller index catchall route, which looks like:

Router::connect('/:controller', array('action' => 'index'), array('defaultRoute' => true));

https://github.com/cakephp/cakephp/blob/2.10.6/lib/Cake/Config/routes.php#L72

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.