views:

190

answers:

3

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?

Thanks!

+1  A: 
Dan
Glad to hear you got a solution, but I think it's because you used the array method (`array('controller' => ..., 'action' => ...)`)
nickf
There's indeed no difference between `$html->` and `$this->Html->`, at least none that has to do with routes.
deceze
+1  A: 

Your solution is no different if you would use an URL like in your example instead of array('controller', 'action').

Solution is to put <base href="<?php echo 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'); ?>" /> into your headers.

This will make all links (also src tags) relative to your webroot. It won't affect JS/CSS URL-s!

sibidiba
As he is working in CakePHP the framework is designed to use helpers. Adding a base tag wouldn't really solve his problem, and would mean sidestepping the framework, which in my opinion is untidy and hacky.
DavidYell
+1  A: 

In general, I would recommend always always always using reverse-routing in your views and controller actions, as a matter of discipline. In my experience, "smart URLs" (e.g. /part_numbers/add) break down very quickly once you start trying to use any of the advanced routing features.

It also violates the principle of writing code once, reading it many times – identifying the controller action invoked by a simple route like /part_numbers/add may be simple, but in a larger application with a ton of custom routes, it becomes much simpler to figure out what action your links will invoke if you use reverse-routing arrays consistently.

Daniel Wright