【问题标题】:laravel Controller method not found in route::controller在 route::controller 中找不到 laravel 控制器方法
【发布时间】:2014-03-15 01:14:05
【问题描述】:

我正在使用Route::controller 查看和编辑表单。在这个动作中 laravel 说:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Controller method not found.

我的路线:

Route::group(array('prefix'=> 'admin' ,'before'=>'auth'), function(){

    Route::controller('profile', 'ProfileController', array('getIndex'=>'profile.index', 'postUpdate'=>'profile.update'));

});

我的表格:

{{ Form::model($profile, array('route' => array('profile.update', $profile->id), 'method' => 'PUT')) }}

{{ Form::close() }}

配置文件控制器:

class ProfileController extends \BaseController {

    public $layout = 'back_end.layouts.main';
    function __construct() {
        $this->beforeFilter('auth', array('except' => array('getIndex', 'postUpdate')));
        $this->beforeFilter('csrf', array('on' => 'post'));
    }    
    public function getIndex()
    {
        if( Auth::check() ){
           $profiles = Auth::user();
          return  View::make('back_end.layouts.profile')->with('profile', $profiles);
        }
        else return Redirect::intended('login');
    }

    public function postUpdate($id)
    {
       if (Session::token() != Input::get('_token'))
       {
            return Response::view('back_end.missing', array(), 404);
       }

       $rules = array(
            'name'        => 'required|alpha',
            'family'      => 'required',
            'email'       => 'required|email',
            'currPassword'=> 'required',
            'password'    => 'required|confirmed',
            'password_confirmation'=>'required',
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails())
        {
           return Redirect::to('/admin/profile')
            ->withErrors($validator)
            ->withInput();
        }
        $id   = Input::get ('id');
        $data = User ::find($id);
        $HashPassowrd = Hash::make(Input::get('password'));

        if( ! Hash::check( Input::get('currPassword') , $data->password ) )
        {
            return Redirect::to('/admin/profile')
            ->withErrors('Current Password Error!');
        }
        else{

            $admin = new User;
            $admin = User::find($id);
            $admin->name     = Input::get('name');
            $admin->family   = Input::get('family');
            $admin->email    = Input::get('email');
            $admin->password = $HashPassowrd;
            $admin->save();

            return Redirect::to('/admin/profile')
            ->withErrors('Edit Successfull');
        }
    }
}

php 工匠路线

+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+
| Domain | URI                                                                    | Name                | Action                                | Before Filters | After Filters |
+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+
|        | GET /                                                                  |                     | Closure                               |                |               |
|        | GET index                                                              | index               | Closure                               |                |               |
|        | GET admin/index                                                        | dashboard           | Closure                               |                |               |
|        | GET logout                                                             | logout              | Closure                               |                |               |
|        | POST auth                                                              | auth                | Closure                               | csrf           |               |
|        | GET login                                                              | login               | Closure                               |                |               |
|        | GET admin/profile/index/{one?}/{two?}/{three?}/{four?}/{five?}         | profile.index       | ProfileController@getIndex            | auth           |               |
|        | GET admin/profile                                                      |                     | ProfileController@getIndex            | auth           |               |
|        | POST admin/profile/update/{one?}/{two?}/{three?}/{four?}/{five?}       | profile.update      | ProfileController@postUpdate          | auth           |               |
|        | GET admin/profile/{_missing}                                           |                     | ProfileController@missingMethod       | auth           |               |
|        | GET admin/manaheHeaders/index/{one?}/{two?}/{three?}/{four?}/{five?}   | manageHeader.index  | ManageHeadersController@getIndex      | auth           |               |
|        | GET admin/manaheHeaders                                                |                     | ManageHeadersController@getIndex      | auth           |               |
|        | POST admin/manaheHeaders/update/{one?}/{two?}/{three?}/{four?}/{five?} | manageHeader.update | ManageHeadersController@postUpdate    | auth           |               |
|        | GET admin/manaheHeaders/{_missing}                                     |                     | ManageHeadersController@missingMethod | auth           |               |
|        | GET test                                                               | test                | Closure                               |                |               |
+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+

【问题讨论】:

  • 也许检查artisan routes 的输出以确保您的URI 和路由名称是正确的。此外,您没有提到(或者我没有看到)错误出现的确切位置 - 是在调用 Form::open() 期间,还是在浏览器中访问 URI(或两者)?如果 Form::open() 调用正常并显示表单,则可能值得检查它生成的 URL,以确保符合您对路由的期望。
  • 你点击的网址是什么?
  • @AntonioCarlosRibeiro 在路由或控制器中?
  • 网址 = http://box.dev/what/url/are/you/hitting?
  • 这是在你发送表单时发生的,还是在 Laravel 试图渲染表单时发生的?通过其他 cmets,看起来表单正在呈现,至少您有一个 URL...只是想确定当您单击按钮发布表单时是否正在发生...

标签: php laravel laravel-4


【解决方案1】:

你有:

 v---- (POST)
POST admin/profile/update/{one?}/{two?}/{three?}/{four?}/{five?}       | profile.update      | ProfileController@postUpdate          | auth           |               |

但是在您使用'method' => 'PUT' 的形式中(在Form::model() 中)所以HTTP 方法不匹配,因此该方法不存在,因为您有postUpdate

控制器方法接受两个参数。第一个是基础URI 控制器处理,而第二个是类名 控制器。接下来,只需向控制器添加方法,前缀为 他们响应的 HTTP 动词

因此,该方法应以put 为前缀或更改默认设置的请求方法POST,以便如果您打算使用POST,IMO 可以从表单中删除该方法。

【讨论】:

  • PUT 更改为POST 或从Form 中删除method 不能解决此问题
  • 你确定这个,你试过了吗?
  • 是的。检查此链接paste.debian.net/82247 我现在收到此错误:Controller method not found.
  • url 中生成了什么Form::modelaction
猜你喜欢
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2014-03-09
  • 2014-05-12
相关资源
最近更新 更多