views:

162

answers:

1

Hi,

I've just started learning cakephp and have gotten the Blog example working except for the routing, I'm still not quite sure how it works after reading many, many documents on routing (including the ones in the official cookbook).

My problem is with the '/' root routing, I want it to go to the index() function of the PostsController so I use:

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

But this doesn't work if I go to the url: localhost/

This is probably (most definetely) because I don't know where to put the cake_1_3 folder/installation, currently my directory tree for localhost (in htdocs) looks as follows:

-htdocs>posts>cake_1_3

This means that when I navigate to: localhost/ I get nothing and when I navigate to: localhost/posts/ I get nothing, just the directory listing for the folder "posts" which shows I have the directory "cake_1_3".

It is only when I go to the url: localhost/posts/cake_1_3/posts/ does the routing work, as in it sees the second "posts" and so runs the "index" function of "PostsController".

Obviously this isn't what I want, I want to be able to go to: localhost/posts/ and it use the index function of the PostsController.

Update: I actually tried taking all the cakephp stuff out of "cake_1_3" and just into "posts" but then I have to go to: localhost/posts/posts/ for it to use the index() function of PostsController.

Is there any way I can just navigate to localhost/posts/ and I'll get the index() function of the PostsController running?

I know this is probably a very simple problem and I'm just missing something because I'm so tired (well that's my excuse anyway), but I've been searching around for about 3 hours now and wouldn't mind a helping hand.

Thanks for your time,

InfinitiFizz

P.S. I've just realised I can dump all the cakephp installation files/folders into the root (htdocs) and then localhost/posts/ will work but I've got loads of different test websites in their own folders in htdocs/ and so I'd rather have this posts test in its own folder too, not have all the cakephp folders mixed up with all the other websites' folders.

+2  A: 

Just to be clear what directories we're talking about, a Cake installation comes with these folders:

/
  app/
    webroot/
  cake

You will have to hit the top / directory with your browser to get a response from Cake at all. All routes are relative to that top / directory.

Let's say you have installed Cake in your web server like so:

/
  htdocs/
    someotherproject/
    mycakeapp/
      app/
        webroot/
      cake/

The htdocs directory is the root of your web server. If you go to http://localhost/, your web server will respond with the contents of /htdocs/. It's not even invoking Cake, so Cake can't route anything.

You'll have to open http://localhost/mycakeapp/ to invoke Cake. From there, Cake will do its routing. The Cake route Router::connect('/', …) corresponds to the URL http://localhost/mycakeapp/. All Cake routes are relative to the app installation path. The Cake routing is app-internal routing, it does not correspond to the absolute URL.

If you want http://localhost/ to be your Cake app, Cake will need to be the only app residing in /htdocs/. You can't have multiple apps in the root and yet have any one of them be "the root app"†‡.

For local development purposes this should be perfectly fine. When uploading the app to a real server with a real domain you'll usually make it the one and only app.


† Well you could, with elaborate rewrite-rules, virtual host configurations or by placing files in Cake's /app/webroot/ folder. Usually more hassle than it's worth though, keep your projects separate.
‡ You can't have your Cake and eat it, too. zing

deceze
Ah okay, I think I was getting confused with '/' being relative then, thanks for the great answer and the hilarious cake-based pun :)
Infiniti Fizz
Actually sorry, I've still got a problem here. If I go to "localhost/posts/" I get redirected to the directory listing for "posts/app/webroot" rather than going where I thought I was routing. I'm using the same Routing as given in my question for the root folder '/' to go to the 'posts' controller's index() function.I thought that would mean going to 'localhost/posts/' takes me there but it doesn't, only 'localhost/posts/posts/' does.(According to your example, replace posts with mywebapp as the folder name).Sorry for unticking but I've just realised this problem.Thanks for your time.
Infiniti Fizz
This also means when I do the "delete" or "edit" links, I get redirected to the 'posts/app/webroot' directory listing, and only when I type in 'localhost/posts/posts/' again do I see the flash() "The post was deleted", so it seems my routing problem is messing up the redirection as well. The redirect code is "$this->redirect(array('action' => 'index'));"Thanks,InfinitiFizz
Infiniti Fizz
@Infiniti So what's your directory structure? `/htdocs/posts/` is the `/htdocs/mycakeapp` from my example? Then the `/posts/posts` is expected. If you're seeing a directory listing for `app/webroot/`, it would seem an `.htaccess` file has gone missing. There are three of those, in `/`, `/app` and `/app/webroot`. Make sure they're there.
deceze
Yeah same except mycakeapp=posts. I've got all three .htaccess files and they are all the same as the default ones from when I unpacked cakephp. Any ideas?Also there is the routes.php problem where it won't redirect to index() of posts_controller when I'm in 'htdocs/posts/' even though '/posts/' is where the app, cake, etc. folders are. routes.php looks as follows: Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Infiniti Fizz
@Infiniti Just to be clear, routes don't *redirect*. Routes are used to determine *which controller and action to invoke for which URL*. `Router::connect('/', array('controller' => 'posts', 'action' => 'index'));` means *"run `PostsController::index()` when somebody visits the URL `/` (`localhost/posts/` in your case)"*. Does that clear things up?
deceze
Ah okay yeah it does thanks. But even that doesn't seem to be working, I just get "Index of /posts/app/webroot" with the directory list rather than the index() function when I go to "localhost/posts/" is this a mod_rewrite problem?
Infiniti Fizz
@Infiniti It would appear so. Cake is not even invoked by the webserver.
deceze
Hmm okay, I've looked through the mod_rewrite stuff in the cakephp book but it seems everything is as it should be. Everything else about cakephp works, I've got some controllers, models and views working together to alter database tables but its just this routing/url problem that's plaguing me at the moment. I suppose I'll just run through all the mod_rewrite stuff again and hope I missed something. Thank you so much for all the help @deceze it is really amazing that you've put so much time and effort into helping me, I hope I can do the same for you some day :) Thanks.
Infiniti Fizz