【发布时间】:2014-01-01 12:46:10
【问题描述】:
我正在使用 Laravel 4.1 开发一个非常基本的应用程序,用户可以在其中注册并提出问题,非常基本的东西。我现在对在 laravel 3 中看起来像这样 public $restful = true 的 restful 方法有点困惑。从那时起,laravel 发生了很大变化,我被这个 restful 想法所困扰。所以我决定离开它,继续开发我的应用程序的框架。一切都很顺利,直到我在 homeController 中创建了 postCreate 方法,让授权用户通过表单提交他们的问题。我相信我正确地路由了该方法,并且 index.blade.php 视图也很好。即使代码似乎没问题,我也无法弄清楚为什么会出现以下错误。
Route [ask] not defined. (View: C:\wamp\www\snappy\app\views\questions\index.blade.php)
如果您在这里发现我做错了什么,请您指出并提供一些解释。 我在 laravel 4 中完全是新手,尽管在以前的版本中有一些经验。
这是我在 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();
}
}
这是我在 routes.php 文件中的内容
<?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
最后在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
请询问您是否需要任何其他代码实例。
【问题讨论】: