我在同一目录APP /控制器中有2个控制器account.php和address.php。对于第一个控制器account.php,我使用URI的默认路由器,如:account / create,account / login,account / logout ...当编写第二个控制器时,我想使用address.php的所有URI以帐号/地址开头/。正如你在这里看到的,我正在使用与帐户控制器相同的URI路由器匹配。
我的第一个方法:
// Add new address
Route::set('address', 'account/address/<action>')
->defaults(array(
'controller' => 'address',
'action' => 'index',
));
我的地址控制器
public function before()
{
parent::before();
if ( ! Auth::instance()->logged_in())
{
// Redirect to a login page (or somewhere else).
$this->request->redirect('');
}
}
// nothing here
public function action_index()
{
$this->request->redirect('');
}
// create account
public function action_create()
{
// Create an instance of a model
$profile = new Model_Address();
// received the POST
if (isset($_POST) && Valid::not_empty($_POST))
{
// // validate the form
$post = Validation::factory($_POST)
->rule('firstname', 'not_empty')
->rule('lastname', 'not_empty')
->rule('address', 'not_empty')
->rule('phone', 'not_empty')
->rule('city', 'not_empty')
->rule('state', 'not_empty')
->rule('zip', 'not_empty')
->rule('country', 'not_empty')
->rule('primary_email_address', 'email');
// if the form is valid and the username and password matches
if ($post->check())
{
if($profile->create_profile($_POST))
{
$sent = kohana::message('profile', 'profile_created');
}
} else {
// validation failed, collect the errors
$errors = $post->errors('address');
}
}
// display
$this->template->title = 'Create new address';
$this->template->content = View::factory('address/create')
->bind('post', $post)
->bind('errors', $errors)
->bind('sent', $sent);
}
似乎没问题,但路由器帐户/地址/创建无效。 Kohana为该URI抛出404消息。
任何人都知道为什么会这样?
答案 0 :(得分:4)
我在我的系统上检查了你的路线并且工作正常,所以我认为错误在其他地方。
检查您的控制器类名是否为Controller_Address
,并尝试将此路由作为引导程序中的第一个路由,docs说:
了解路线在订单中是否匹配非常重要 它们被添加,一旦URL匹配路由,路由就是 基本上“停止”,其余路线从未尝试过。 因为默认路由几乎匹配任何内容,包括空 url,新路线必须放在它之前。
另一件事,与问题无关 - 你真的应该在你的模型中放置验证。控制器不适合它;)