0

今日は文字通りLaravelをダウンロードして、見た目が好きですが、2つのことに苦労しています。

1)ルートを使用する代わりにURLを分析するコントローラーのアクション方法が好きで、すべてをよりきれいにまとめているように見えますが、行きたいとしましょう

/account/account-year/

このためのアクション関数を作成するにはどうすればよいですか?すなわち

function action_account-year()...

明らかに有効な構文ではありません。

2)私が持っていた場合

function action_account_year( $year, $month ) { ...

訪問しました

/account/account_year/

引数の欠落についてエラーが表示されますが、このユーザーフレンドリーにする/差分ページをロードする/エラーを表示するにはどうすればよいですか?

4

3 に答える 3

8

ハイフンでつながれたバージョンを手動でルーティングする必要があります。

Route::get('account/account-year', 'account@account_year');

パラメータに関しては、ルーティング方法によって異なります。ルートのパラメータを受け入れる必要があります。フルコントローラールーティング(例Route::controller('account'))を使用している場合、メソッドにはパラメーターが自動的に渡されます。

手動でルーティングする場合は、パラメータをキャプチャする必要があります。

Route::get('account/account-year/(:num)/(:num)', 'account@account_year');

だから訪問/account/account-year/1/2はするだろう->account_year(1, 2)

お役に立てれば。

于 2013-01-10T22:01:00.377 に答える
4

次の可能性も考えられます

class AccountController extends BaseController {

    public function getIndex()
    {
        //
    }

    public function getAccountYear()
    {
        //
    }

}

ここで、次の方法でルートファイルにRESTfulコントローラーを定義するだけです。

Route::controller('account', 'AccountController');

訪問'account/account-year'すると、アクションに自動的にルーティングされますgetAccountYear

于 2013-02-15T14:47:33.690 に答える
0

他の誰かがそれを探している場合に備えて、私はこれを答えとして追加すると思いました:

1)

public function action_account_year($name = false, $place = false ) { 
     if( ... ) { 
             return View::make('page.error' ); 
     }
}

2)

まだ固溶体ではありません:

laravel / routing / controller.php、メソッド「応答」

public function response($method, $parameters = array())
{
    // The developer may mark the controller as being "RESTful" which
    // indicates that the controller actions are prefixed with the
    // HTTP verb they respond to rather than the word "action".

    $method = preg_replace( "#\-+#", "_", $method );            

    if ($this->restful)
    {
        $action = strtolower(Request::method()).'_'.$method;
    }
    else
    {
        $action = "action_{$method}";
    }

    $response = call_user_func_array(array($this, $action), $parameters);

    // If the controller has specified a layout view the response
    // returned by the controller method will be bound to that
    // view and the layout will be considered the response.
    if (is_null($response) and ! is_null($this->layout))
    {
        $response = $this->layout;
    }

    return $response;
}
于 2013-01-10T23:53:55.390 に答える