【问题标题】:Post method not working in laravel 4发布方法在 laravel 4 中不起作用
【发布时间】:2014-04-10 12:05:23
【问题描述】:

我从 Laravel 4 开始,当我提交表单时,我收到错误:NotFoundHttpException。

routes.php

<?php

// We defined a RESTful controller and all its via route directly
Route::controller('feeds', 'FeedsController');

Route::get('/', array('as' => 'index', 'uses' => 'FeedsController@getIndex'));

FeedsController.php

//The method to show the form to add a new feed
public function getCreate() {
    //We load a view directly and return it to be served
    return View::make('create_feed');
}

//Processing the form
public function postCreate() {

    //Let's first run the valiadtion with all provided input 
   $validation = Validator::make(Input::all(), Feeds::$form_rules);

    //If the validation passes, we add the values to the database and return to the form
    if($validation->passes()) {
        //We try to insert a new row with Eloquent
        $create = Feeds::create(array(
                                    'feed' => Input::get('feed'), 
                                    'title' => Input::get('title'), 
                                    'active' => Input::get('active'), 
                                    'category' => Input::get('category')
            ));
        //We return to the form with success or error message due to state of the
        if($create) {
            return Redirect::to('feeds/create')->with('message', 'The feed added to the database successfully!');
        } else {
            return Redirect::to('feeds/create')->withInput()->with('message', 'The feed could not be added, please try again later!');
        }
    } else {
        //If the validation does not pass, we return to the form with first error message as flash data
        return Redirect::to('feed/create')->withInput()->with('message',     $validation->errors()->first());
    }
}

create_feed.blade.php

@if(Session::has('message'))
    <h2>{{Session::get('message')}}</h2>
@endif

{{Form::open(array('url' => 'feeds/create', 'method' => 'post'))}}

<h3>Feed Category</h3>
    {{Form::select('category',array('News'=>'News','Sports'=>'Sports','Technology'=>'Technology'),Input::old('category'))}}

<h3>Title</h3>
{{Form::text('title',Input::old('title'))}}

<h3>Feed URL</h3>
{{Form::text('feed',Input::old('feed'))}}

<h3>Show on Site?</h3>
{{Form::select('active',array('1'=>'Yes','2'=>'No'),Input::old('active'))}}

{{Form::submit('Save!',array('style'=>'margin:20px 100% 0 0'))}}


{{Form::close()}}

php 工匠路线

|        | GET feeds/create/{one?}/{two?}/{three?}/{four?}/{five?}  |       | Fe
edsController@getCreate     |                |               |
|        | POST feeds/create/{one?}/{two?}/{three?}/{four?}/{five?} |       | Fe
edsController@postCreate    |                |               |
|        | GET feeds/index/{one?}/{two?}/{three?}/{four?}/{five?}   |       | Fe
edsController@getIndex      |                |               |
|        | GET feeds                                                |       | Fe
edsController@getIndex      |                |               |
|        | GET feeds/{_missing}                                     |       | Fe
edsController@missingMethod |                |               |
|        | GET /                                                    | index | Fe
edsController@getIndex      |                |               |

当我提交表单时,我收到错误:NotFoundHttpException 和 url 重定向:feed/create。我不明白。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    尝试改变这一行

     Route::controller('feeds', 'FeedsController');
    

    Route::resource('feeds', 'FeedsController');
    

    在你的 routes.php 中

    【讨论】:

    • 我尝试过 create_feed.blade.php 没有加载。
    【解决方案2】:

    您的控制器并不安静。如果您曾经使用过 artisan,那么您可以使用它来生成您的控制器,该控制器将生成它作为 restful。它将为您提供 index()、create() store() 等方法。这些是您应该使用的 restful 方法。

    通过将您的代码放入您认为合适的每个位置,然后在路线上您应该使用Route::resource('feeds', 'FeedsController')

    如果您需要生成自己的方法,因为这些方法都不适合您正在做的事情,那么您应该明确告诉您的路线。例如Route::get('/', array('as' =&gt; 'index', 'uses' =&gt; 'FeedsController@getIndex'));.

    【讨论】:

    • 要使用 Artisan 创建控制器,您需要使用命令或终端 cd 进入您的 laravel 目录。然后你使用 php artisan controller:make ControllerName。 laravel.com/docs/controllers
    • 我使用的是:php artisan controller:make FeedsController 来创建我的控制器,我的表单:
      localhost/laravel-blueprint/news/public/feeds/create"accept-charset="UTF-8"> .但是当提交 url 是:feed/create 并得到错误。
    • 尝试使用点表示法而不是正斜杠,而不是 url,您可以尝试使用路由或控制器。 {{ Form::open(array('route' => 'feeds.create')) }}
    • 我尝试得到错误:Route [feeds.create] not defined
    猜你喜欢
    • 2013-03-23
    • 2014-11-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2018-12-15
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多