【问题标题】:Kohana 3.2 set router for a controller which has a prefix URI belongs to another controllerKohana 3.2 为具有前缀 URI 的控制器设置路由器属于另一个控制器
【发布时间】:2012-04-03 21:31:02
【问题描述】:

我在同一目录 APP/controller 中有 2 个控制器 account.php 和 address.php。对于第一个控制器 account.php,我对 URI 使用默认路由器,例如:account/create、account/login、account/logout... 在编写第二个控制器时,我想使用 address.php 的所有 URI 以 account/address 开头/。正如您在此处看到的,我使用与帐户控制器相同的 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 消息。

有人知道为什么会这样吗?

【问题讨论】:

    标签: controller kohana router kohana-orm kohana-3.2


    【解决方案1】:

    我在我的系统上检查了你的路线,它工作正常,所以我认为错误出在其他地方。 检查您的控制器类名称是否为Controller_Address,也尝试将此路由作为引导程序中的第一个,如docs 所说:

    了解路由按顺序匹配很重要 它们被添加,一旦 URL 与路由匹配,路由就会 基本上“停止”,其余路线从未尝试过。 因为默认路由几乎匹配任何东西,包括一个空的 url,新的路由必须放在它前面。

    另一件事,与问题无关 - 您确实应该在模型中放置验证。控制器不是它的地方;)

    【讨论】:

    • 了不起的人,把它放在首位就可以了。是的,你是对的,验证应该在模型中,它会让我们的控制器看起来干净和可读。
    猜你喜欢
    • 2023-02-09
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多