Internal Navigation

Internal navigation

Internal navigation在浏览器中用指定的URL开始。我们假设URL以server name和项目的名字开始的,例如: http://www.mysite.com/myproject.

假设你还没有更改默认的值,在config/routes.php的第一行,你会发现:

Map->connect('/:controller/:action/:id', array('controller' => 'page', 'action' => 'index'));

这条路径使得你的URLs象这样运行:

你可以通过添加controller名来扩展你的URL,一个动作和一个id: http://www.mysite.com/myproject/controller/action/id或者 http://www.mysite.com/myproject/controller/action. 一些actions需要id,有一些不需要。如果你不指定controller,那么默认的controller,根据上面的默认值,是”page”,他是一个controller,在app/controllers/page_controller.php. (你也许会替代默认值”page” ,而改用你的controller。)

Controller

每个controller包含一个或者多个actions。根据默认路径,如果没有action被指定,缺省的action是”index”,所以每个controller最好都有一个index函数。每个action也许指向另一个action 。如果没有,action的执行是通过view的执行实现的。view在 app/views目录下。他对于每一个controller他都包含了一个子目录。user_controller.php文件在app/views/user目录下。在那个目录下会有对应每个action的文件存在。如果编写的action是为了指向另一个action或者另一个controller/action,那你就不需要view文件。让我们来说一个”user” controller的 “index” action重定向到”login” method。那你就不需要app/views/user/index.tpl文件,但是你需要一个app/views/user/login.tpl文件。

在controller里面我们提及了从一个action到另一个action的重定向功能。如果你想直接去另一个action我们可以用语句这样作:

$this->renderAction('home');

在这个例子里,你最好在controller有一个名为home的function。在另一个 controller想重定向到一个action或者那个action需要id(参考默认路径), 用类似下面的语句:

$this->redirectTo(array('controller' => 'user','action' => 'edit', 'id' => $this->User->getId()));


经常来说一个controller内的flow从一个action转向一个view然后又重新回到同一个action。在这个例子中,你必须能够知道是否为这个view设置数据或者是否处理这个view中的数据。作这个的方法是:对controller来说有这样的一个数组$this→params['controller_name']。他保存着view中的数据。因此,如果他是空的,可能我们还没有这个view。另一种测试的方法是 $this→Request→isPost().。但OK按钮被按下之后,通过HTTP Post数据从 view返回到controller。</sup>. 因此,如果是真的,数据将被返回。在下面的例子中,当isPost()为真时,我们也想执行model's save()功能。经常,这个例子会显示: flash a notice and redirect action to another action.

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 当一个model生成之后,当然也会有这个文件app/helpers/model_helper.php。在这个文件中的代码决定了当view中一个button被按下之后什么将会发生。默认情况下,对于”OK” button来说会返回到定义这个form的controller的action 。对于”Cancel” button 来说会去”listing” function.

Views

在app/views/目录下,对应于每一个controller都有一个目录。文件当中会有一些tpl文件,和controller中的action是一一对应的。(这些view文件会放在PHP files中被编译。)在这些文件中也许有导航控制语句。

在view中的第一种导航语句是链接,这些链接可以选择去任何地方。一个例子是

<?php  echo $url_helper->link_to($text_helper->translate('Log out'), array('action' => 'login'))?>

他会定向到当前controller的login方法。

其他种类的view navigation和form一起使用。他指定了当OK按钮被按下之后,他去那个controller并且这个数据被保存。他会象这样:

<?php  echo  $form_tag_helper->start_form_tag(array('action'=>'add')) ?>
 
internal-navigation_cn.txt · Last modified: 2008/07/10 09:25 by liyh
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki