2014-01-01 12:46
浏览 335

看似正确的路由获取错误路由未定义! - Laravel 4

I'm developing a very basic application using Laravel 4.1 where users can signup and ask question, pretty basic stuffs. I'm now a bit confused about the restful method which would look something like this public $restful = true in laravel 3. Since then laravel has changed a lot and I got stucked with the restful idea. So I decided to leave it and go on developing the skeleton of my application. Everything went well until I created the postCreate method in my homeController to let authorized users submit their question through a form. I believe I routed the method correctly and the index.blade.php view is alright as well. I just can't figure out why I'm getting this following error even though the codes seem to be okay.

Route [ask] not defined. (View: C:\wamp\www\snappy\app\views\questions\index.blade.php)

If you have got what I'm doing wrong here would appreciate if you point it out with a little explanation. I'm totally new in laravel 4 though had a bit of experience in the previous version.

Here's what I have in the HomeController.php


class HomeController extends BaseController {

    public function __construct() {

        $this->beforeFilter('auth', array('only' => array('postCreate')));

    public function getIndex() {

        return View::make('questions.index')
            ->with('title', 'Snappy Q&A-Home');

    public function postCreate() {

        $validator = Question::validate(Input::all());

        if ( $validator->passes() ) {
            $user = Question::create( array (
                'question' => Input::get('question'),
                'user_id' => Auth::user()->id

            return Redirect::route('home')
                ->with('message', 'Your question has been posted!');

        return Redirect::route('home')


this is what I have in the routes.php file


Route::get('/', array('as'=>'home', 'uses'=>'HomeController@getindex'));
Route::get('register', array('as'=>'register', 'uses'=>'UserController@getregister'));
Route::get('login', array('as'=>'login', 'uses'=>'UserController@getlogin'));
Route::get('logout', array('as'=>'logout', 'uses'=>'UserController@getlogout'));

Route::post('register', array('before'=>'csrf', 'uses'=>'UserController@postcreate'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UserController@postlogin'));
Route::post('ask', array('before'=>'csrf', 'uses'=>'HomeController@postcreate')); //This is what causing the error

And finally in the views/questions/index.blade.php



    <div class="ask">

        <h2>Ask your question</h2>

        @if( Auth::check() )

            @if( $errors->has() )
                <p>The following erros has occured: </p>

                <ul class="form-errors">
                    {{ $errors->first('question', '<li>:message</li>') }}

            {{ Form::open( array('route'=>'ask', 'method'=>'post')) }}

                {{ Form::token() }}

                {{ Form::label('question', 'Question') }}
                {{ Form::text('question', Input::old('question')) }}

                {{ Form::submit('Ask', array('class'=>'btn btn-success')) }}

            {{ Form::close() }}


    <!-- end ask -->


Please ask if you need any other instance of codes.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanmei1536 2014-01-01 12:53

    you are using name route ask in your form which is not exist. I have created the name route ask for you.

    Route::post('ask', array('before'=>'csrf', 'as' => 'ask', 'uses'=>'HomeController@postcreate'));
    {{ Form::open( array('route'=>'ask', 'method'=>'post')) }}
                           ^^^^ -> name route `ask`
                    {{ Form::token() }}
    打赏 评论
  • doure8758 2014-01-01 12:52

    Your 'ask' route is not named. When you pass 'route' => 'foo' to Form::open, that assumes you have a route named 'foo'. add 'as' => 'ask' to your /ask route and it should work.

    Alternatively, use URL or Action to resolve the form's target url instead:

    Form::open(['url' => 'ask']);
    Form::open(['action' => 'HomeController@postCreate']);
    打赏 评论

相关推荐 更多相似问题