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条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面