===== Internal navigation ===== Internal navigation begins with the URL specified in the browser. We will assume that the URL begins with the server name and the name of the project: http://www.mysite.com/myproject. Assuming that you haven't changed the default, the first line in config/routes.php, you will have: Map->connect('/:controller/:action/:id', array('controller' => 'page', 'action' => 'index'));That route will make your URLs act like this: You may extend the URL by adding the name of a controller, an action and an id: http://www.mysite.com/myproject/controller/action/id or http://www.mysite.com/myproject/controller/action. Some actions require an id, some don't. If you don't specify a controller, the default controller, according to the above route default, will be "page", which is app/controllers/page_controller.php. (You'll probably want to replace the "page" default in routes.php with a controller that you write.) ==== Controller ==== Each controller contains one or more functions (actions). According to the default route, if no action is specified, the implied action is "index", so each controller had better have an index function. Each action may specify another action to be taken. If it doesn't, execution of the action will be followed by executing a view. The view will be in the app/views directory. It contains one subdirectory for each controller. The user_controller.php file will go to the app/views/user directory. Within that directory will be one file for each action. If you program an action so that it always goes to another action or another controller/action, you won't need a view file. Let's just say that the "user" controller's "index" action redirects to the "login" method. You won't need an app/views/user/index.tpl file, but you will need an app/views/user/login.tpl file. We mentioned the ability to redirect an action from one action to another in the controller code. If we wish to go directly to another action, we can do it with a statement like: $this->renderAction('home');In this case, you'd better have a function named home in the controller. To redirect to an action within another controller or to an action that requires an id (reference the default route), use a statement similar to this: $this->redirectTo(array('controller' => 'user','action' => 'edit', 'id' => $this->User->getId()));\\ Very often the flow of control goes from the action in the controller to the view and back to the same action. In this case, you must be able to know whether to set up data for the view or whether to process data from the view. The way to do it is this: There is an array for the controller called ''$this->params['controller_name']''. It holds the data from the view. Therefore, if it is empty, we haven't been to the view yet. Another test is ''$this->Request->isPost()''. Data is returned to the controller from a view via an HTTP Post when the OK button is pressed1. Therefore, if this is true, data has been returned. In the example below, when isPost() is true we also want to execute (and test for success) the model's ''save()'' function. Often, we will do what this example shows: flash a notice and redirect action to another action. [[flash-communicating-between-actions|More information on flash is here.]] if(empty($this->params['user'])) { // Set up data for the view } else { // Process data from the view $this->user->setAttributes($this->params['user']); if($this->Request->isPost() && $this->user->save()){ $this->flash['notice'] = $this->t('User was successfully updated.'); $this->redirectTo(array('action' => 'show', 'id' => $this->user->getId())); } } Notes:\\ 1 When a model is generated, so also is a file called app/helpers/model_helper.php. In this file is the code that determines what happens when a button in a view is pressed. The default for an "OK" button is to go to the controller function (action) defined in the declaration of the form. The default for a "Cancel" button is to go to the "listing" function.\\ ==== Views ==== Within the app/views/ directory will be a directory for each controller. Within each of those will be a bunch of tpl files, one corresponding to nearly (see above) each method (function) in the controller. (These view files will be "compiled" into PHP files.) Within these files may be navigation controlling code. The first kind of navigation code in a view file are links that the user may choose to go elsewhere than the page he is looking at. An example is link_to($text_helper->translate('Log out'), array('action' => 'login'))?> This will redirect to the login method of the current controller. The other kind of view navigation code is for use with a form. It specifies where in the controller action is to go after an OK button is pressed and the data saved. It may look something like this: start_form_tag(array('action'=>'add')) ?>