Why we are using routing in cakePHP and what would be the basic approaches for implementation...?
Routing allows aliasing and routing(!) of URLs. It gives us a cleaner, more controlled interface and underpins the functioning of CakePHP.
The first step would be to read the appropriate chapter in the book: http://book.cakephp.org/view/542/Defining-Routes (1.2) or http://book.cakephp.org/view/948/Defining-Routes (1.3)
Then look at the routes.php file (app/config/routes.php) to understand how it goes together.
Finally, when you know what you want to do (we don't because you haven't told us), try it debug it and use it.
Why
Because it allows you to decouple your URLs from your controller actions. You can name your controllers and actions in a way that makes sense internally, and invoke them using URLs that do not need to bear any resemblance to your internal naming scheme.
FooApiVersion1Controller::internal_beta_method() can be invoked by the URL /api/v1/method, and you can swap out the controller or method at any time without needing to change the URL.
How
Read the manual. http://book.cakephp.org/view/945/Routes-Configuration
http://book.cakephp.org/view/310/Configuration
<?php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
?>
Related
I have a site that uses CakePHP 2.x. There's a backend interface where actions use the standard Cake layouts and views, but several of the actions are also exposed to front end users as "dialogs" (same functionality, just a layout that can be put in iframe).
In app/Config/router.php I have added the following:
Router::connect('/dialog/:controller', array('action' => 'index'));
Router::connect('/dialog/:controller/:action');
Router::connect('/dialog/:controller/:action/**');
This works appropriately, but the problem starts when trying to use the HTML helper's link() method. If I try to create a link like:
$this->Html->link('edit account', array('controller' => 'users', 'action' => 'edit'));
I get the following:
edit account
When the link is within a dialog, this works great, but I don't want the non-dialog pages to link to the dialog.
How can I control which of the two URLs is used in a particular page?
Is there something I can call from within AppController once I know whether the page being rendered is a dialog or not, or even something in the call to link() that would allow me to override it.
I know there's the "prefix" option which would allow for URLs like /user/dialog_edit but I would like to maintain the /dialog/users/edit format if possible. I also know I can hard code the URL vs. passing controller/action/id/etc in an array, and I don't anticipate pathing/model names changing, but I'd like to do this the idiomatic way for CakePHP, if possible.
I'm making a cakephp 2.3 app and have these "pages":
mysite.com/demo
mysite.com/admin
The above are fixed. The below are generated:
mysite.com/johnsmith
mysite.com/cooluser88
I've updated the routes with:
Router::connect(
'/*/',
array('controller' => 'pages', 'action' => 'redirect')
);
Am I on the right track? a redirect method in the pages controller? I'm using the official docs but I haven't been able to find specific examples.
I'll keep working on it and will update with an answer if I find one!
Your best bet IMO is to do something like reddit where each slug is prefixed with /r/.
Example:
mysite.com/u/johnsmith
Then, in your routes, you can do something like this:
Router::connect('/u/:uname/*', array('controller'=>'users', 'action'=>'display'),
array('pass'=>array('uname')));
If you really don't want to do that, you can use something like:
Router::connect('/:uname/*', array('controller'=>'users', 'action'=>'display'),
array('pass'=>array('uname')));
But keep in mind, any other controllers, plugins...etc etc etc will need their own routes, since it will think that ANYTHING passed after the mysite.com/ is a uname. Not the best idea IMO.
I am very new to cakephp.
I have my project 'registration' in the workspace. I have created an IndexController, which contains method index().
When I run my project by using workspace/registration/ it displays the following error:
Error: WorkspaceController could not be found. Create the class WorkspaceController below in file: app/Controller/WorkspaceController.php.
I want to exicute IndexController first. Somebody advised me to change the default route. But I dont know how to change the route. Please help me.
CakePHP has a fantastic documentation -> cookbook
And to your question, did you try to do what they suggested? Creating the class WorkspaceController in Controller!
In app/Config/routes.php:
Router::connect(
'/workspace/registration',
array('controller' => 'index', 'action' => 'index')
);
If /workspace/registration is where you register to join a workspace, though, I'd really recommend creating a WorkspaceController to deal with all workspace-related functionality.
Work with the default routes, not against them, it will make your life a lot easier.
I would like to be able to route something like the following in CakePHP 2.0:
domain.com/london
domain.com/milton keynes
to a specific controller and action.
The application has multiple controllers so it should only use this route if the parameter provided doesn't match a controller name.
I achieved this with CakePHP 1.3.12 by adding the following code to the bottom of config/routes.php
Router::connect(
'/:location',
array('controller' => 'articles', 'action' => 'testing'),
array('pass' => array('location'), 'location' => '[a-z ]+')
);
Using this code with CakePHP 2.0 only works if I comment out the require line from config/routes.php, but then I loose the default routes so that a URL pointing at any other controller is caught by this.
How can I achieve the desired routing?
As far as I know this shouldn't work in Cake 1.3 either, simply because your [a-z ]+ regex also matches the case for a simple /controller_name route; the Router has no way to distinguish between the two and will thus always route to the one it encounters first.
You can, however, create a custom route class to achieve this. Mark Story (one of the Cake devs) wrote an excellent post about it quite some time ago, it's for Cake 1.3 but you can easily apply the principle to 2.0 (I know 'cause I'm using it in my 2.0 app). You can the find the post here.
This might not answer the specific question asked above, but it's relevant, this page comes up in search results, and my goal is to save whoever finds it (including future me I guess) some time on not having to research what I just researched.
I needed to add routing with a parameter for a different domain. For example, example.com should behave as usual, while example.org/some_page should route directly to a specific controller and action. So I've added the following to my Config/routes.php:
if ( CakeRequest::host()=='example.org' ) {
Router::connect('/:my_variable',
array(
'controller'=>'my_controller',
'action'=>'my_action'
),
array(
'my_variable'=>'[a-zA-Z-0-9 ]+',
'pass'=>array('my_variable')
)
);
}
Im new to cakePHP, and Im wondering how a 'live' site does this.
I see 2 possibilities :
1) There is one controller with a bunch of pages (functions) in its (extended) AppController.
2) There are many controllers, each with a small number of pages (functions) in its (extended) AppController.
(You probably get my question already, but Im going to say it in another way too)
Should I put my contact page in a separate controller than my blog page? (I have a hunch the answer is yes.) Why?
You don't need to create a controller for everything. In fact, you shouldn't, because there are better ways around it. The more static pages you have, the more out of hand it can get.
For Static Pages
Copy pages_controller.php from the cake/libs/controller folder over to your app/controllers folder. Add the following piece of code to your display() action:
function display() {
...
$page = Inflector::slug($page);
if (method_exists($this, $page)) {
$this->$page();
}
$this->render(join('/', $path));
return;
}
Then, modify your routes.php file to add the various static pages:
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));
Now, the contact form is a static page, but has some logic attached to it. So, you can head over to your PagesController and create an action specifically for this (or any other page that isn't merely static):
function contact() {
if (!empty($this->data)) {
...
}
}
Basically, the route directs your static page request to the PagesController's display() action. The display action checks if a method of the same name exists. If it does, it executes that action and displays the pages/{page}.ctp view.
For Non-Static Pages, eg. Blog
Now, this definitely needs a model. In fact, multiple models (Post hasMany Comment, Post HABTM Tag). In order to manipulate and access these different models, it's better that you place the code into a separate controller.
A lot of people like to name their controllers based on their URLs. For example, they name their controller as BlogController if they want a URL such as /blog.
A method that I prefer is using routing to get the URLs that I want, and keeping controllers named as per CakePHP conventions.
Eg. A PostsController would control the Post model and related models. But if I wanted the /blog URL to display a list of all the posts, I would write a route to point it to /posts/index.
Router::connect('/blog', array('controller' => 'posts', 'action' => 'index'));
You can have additional routes too. Example: /blog/2010/06/10/whats-in-a-post to point to /posts/view/.
Again, this is just an example of what's possible. In the end, you should stick to the methods that you think helps keep your code organized for both you and your team.