在看似正确的路由后得到错误Route not defined!- Laravel 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20868782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upon seemingly correct routing getting error Route not defined! - Laravel 4
提问by satancorpse
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.
我正在使用 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)
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.
如果你有什么我在这里做错了,如果你能用一点解释指出它,我将不胜感激。我是 laravel 4 的新手,虽然在以前的版本中有一些经验。
Here's what I have in the HomeController.php
这是我在 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
这是我在 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
And finally in the views/questions/index.blade.php
最后在 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.
请问您是否需要任何其他代码实例。
回答by Andreas
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.
您的“询问”路线未命名。当您传递'route' => 'foo'
到Form::open
时,假设您有一个名为“foo”的路由。添加'as' => 'ask'
到您的 /ask 路线,它应该可以工作。
Alternatively, use URL or Action to resolve the form's target url instead:
或者,使用 URL 或 Action 来解析表单的目标 URL:
Form::open(['url' => 'ask']);
Form::open(['action' => 'HomeController@postCreate']);
回答by Anam
you are using name route ask
in your form which is not exist. I have created the name route ask
for you.
您ask
在表单中使用了不存在的名称路由。我已经ask
为你创建了名称路由。
Route::post('ask', array('before'=>'csrf', 'as' => 'ask', 'uses'=>'HomeController@postcreate'));
{{ Form::open( array('route'=>'ask', 'method'=>'post')) }}
^^^^ -> name route `ask`
{{ Form::token() }}