Search code examples
phpcakephphyperlinkhtml-helperrelative-path

Links not relative in cakephp


In my layout, I have a menu that I've included as an element, and it contains a link like so.

<? $html->link('New Part Number','/part_numbers/add'); ?>

The problem that I have is that cake isn't redirecting correctly and it ends up sending me to "http://localhost/part_numbers/add" instead of "http://localhost/my_client_folder/client_app/part_numbers/add" (I'm building this locally). My CakePHP app is stored a few directories below my webroot folder, but I thought CakePHP would autodetect how to build the linking no matter where the application was located, right?

So do I need to configure the application root folder, or create a route or something?


Solution

  • Thank you everyone for your solutions, my previous solution was indeed in error:

    You have to build it off of "$this" so that it knows where you're coming from, otherwise it can't figure out how to build a relative link.

    Html->link("New Part Number", array('controller' => 'part_numbers', 'action' => 'add')); ?>

    The REAL reason that the links were not working, as kindly mentioned below, was because of not specifying the array. This should work to fix the link:

    <?= $html->link("New Part Number", array('controller' => 'part_numbers', 'action' => 'add')); ?>