cakephp routing problem (or maybe just confusion)
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 c开发者_如何学JAVAan 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.
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
精彩评论