dpd66100 2014-01-01 12:46
浏览 346
已采纳

看似正确的路由获取错误路由未定义! - 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

<?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')
            ->withErrors($validator)
            ->withInput();
    }

}

this is what I have in the routes.php file

<?php

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

@extends('master.master')

@section('content')

    <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>') }}
                </ul>
            @endif

            {{ 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() }}

        @endif

    </div>
    <!-- end ask -->

@stop

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() }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)