Laravel 5:在RouteCollection.php

时间:2015-11-11 12:39:33

标签: php laravel authentication

希望有人可以提供帮助。

在此阶段,我有其他所有方法(编辑和新方法)都能很好地处理受保护的路由。它只是删除不好玩。 (虽然我确定这是我做的傻事)。我已经删除了auth,删除了路由组,并为其分配了中间件,并成功删除了配置(对象),因此操作正常。我尝试按照另一篇文章中的建议在firebug中检查它,然后我得到:

405 Method Not Allowed

Allow           DELETE
Cache-Control   no-cache, private
Connection      close
Content-Type    text/html
Date            Wed, 11 Nov 2015 12:36:40 GMT
Host            127.0.0.1:8000
X-Powered-By    PHP/5.5.9-1ubuntu4.14

我希望用户在创建新条目或编辑现有条目之前进行身份验证(同样也可以进行删除)。

当我按下按钮尝试删除时,它会指示我进行身份验证/登录(开箱即用的快速入门版本),但是在成功登录后,地址栏中的网址为:< / p>

127.0.0.1:8000/delete/48

表示:

MethodNotAllowedHttpException in RouteCollection.php line 219:

routes.php:

Route::get('/new', [
    'as' => 'create', 
    'middleware' => 'auth',
    'uses' => 'ConfigsController@getCreate'
]);
$router->group(['middleware' => 'auth'], function($router)
    {
    Route::get('{id}/edit', 'ConfigsController@edit');
    Route::delete('/delete/{id}/', 'ConfigsController@getDL');
    });

+--------+----------+-------------------+----------+-------------------------------------------------------+------------+
| Domain | Method   | URI               | Name     | Action                                                | Middleware |
+--------+----------+-------------------+----------+-------------------------------------------------------+------------+
|        | DELETE   | delete/{id}       |          | App\Http\Controllers\ConfigsController@getDL          | auth       |
|        | POST     | new               |          | App\Http\Controllers\ConfigsController@postCreate     |            |
|        | GET|HEAD | new               | create   | App\Http\Controllers\ConfigsController@getCreate      | auth       |
|        | GET|HEAD | {id}/edit         |          | App\Http\Controllers\ConfigsController@edit           | auth       |

ConfigsController.php:

public function getCreate() {
return view('create');
}

public function edit($id)
{
$configs = Config::find($id);
    return view('edit')
    ->with('configs', $configs);
}

public function getDL($id) {
    $configs = Config::find($id)->delete();
    return Redirect::to('/');
}

index.blade.php (edit & delete) & app.blade.php (new):

<ul class="nav navbar-nav">
  <li><a href="{{ URL::route('create') }}">New</a></li>
</ul>

{!! Form::open(array('url' => '/delete/' . $config->id . '/', 'class' => 'pull-right')) !!}
{!! Form::hidden('_method', 'DELETE') !!}
{!! Form::submit('Delete this Config', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}

<a class="btn btn-small btn-info" href="{{ URL::to('/' . $config->id . '/edit') }}">Edit this Config</a>

1 个答案:

答案 0 :(得分:0)

这是因为Laravel authenticate()功能开箱即用,在重定向时使用“有意”。

这意味着,当用户未登录并在/whateverpage Laravel上执行get请求并强制用户登录时,Laravel成功登录后会将用户重定向到我们示例中的“预期”页面/whateverpage以及您的/delete/48

/**
* Send the response after the user was authenticated.
*
* @param  \Illuminate\Http\Request  $request
* @param  bool  $throttles
* @return \Illuminate\Http\Response
*/
protected function handleUserWasAuthenticated(Request $request, $throttles)
{
    if ($throttles) {
        $this->clearLoginAttempts($request);
    }

    if (method_exists($this, 'authenticated')) {
        return $this->authenticated($request, Auth::user());
    }

    return redirect()->intended($this->redirectPath());
}

此代码位于trait内部,因此您正在寻找"override-ing" trait函数。

使用$redirectPath

解决方案

this documentation section,您可以看到设置$redirectPath就是您所需要的。

  

当用户成功通过身份验证后,他们将被重定向到/ home URI,您需要注册要处理的路由。您可以通过在AuthController上定义redirectPath属性来自定义验证后重定向位置:protected $redirectPath = '/dashboard';

解决方案和原因

您获得MethodNotAllowedHttpException,因为浏览器只能执行GET或POST请求; here是来自Laravel文档(表单方法欺骗)的更多信息。

  

HTML表单不支持PUT,PATCH或DELETE操作。因此,在定义从HTML表单调用的PUT,PATCH或DELETE路由时,您需要向表单添加隐藏的_method字段。

由于您没有使用表单来删除“配置”,因此您必须使用GET而不是DELETE

<击> Route::delete('/delete/{id}/', 'ConfigsController@getDL'); Route::get('/delete/{id}/', 'ConfigsController@getDL');

工作示例

$ php artisan route:list

+--------+----------+--------------------+---------------+--------------------------------------------------+------------+
| Domain | Method   | URI                | Name          | Action                                           | Middleware |
+--------+----------+--------------------+---------------+--------------------------------------------------+------------+
|        | GET|HEAD | config/create      |               | App\Http\Controllers\ConfigController@getCreate  | auth       |
|        | POST     | config/create      |               | App\Http\Controllers\ConfigController@postCreate | auth       |
|        | GET|HEAD | config/{id}/delete | config.delete | App\Http\Controllers\ConfigController@destroy    | auth       |
|        | GET|HEAD | config/{id}/edit   |               | App\Http\Controllers\ConfigController@getEdit    | auth       |
|        | POST     | config/{id}/edit   |               | App\Http\Controllers\ConfigController@postEdit   | auth       |
+--------+----------+--------------------+---------------+--------------------------------------------------+------------+

<强> routes.php文件

Route::group(['middleware' => 'auth'], function () {
    Route::group(['prefix' => 'config'], function() {
        Route::get('create', 'ConfigController@getCreate');
        Route::post('create', 'ConfigController@postCreate');

        Route::get('{id}/edit', 'ConfigController@getEdit'); 
        Route::post('{id}/edit', 'ConfigController@postEdit');

        Route::get('{id}/delete', ['as' => 'config.delete', 'uses' => 'ConfigController@destroy']);
    });
});

和控制器(注意它被重命名为 ConfigController.php

<?php namespace App\Http\Controllers;


use App\Config;

class ConfigController extends Controller {

    public function index()
    {
        // just show and sort, paginate... all config(s)
    }

    public function getCreate() {
        return view('create');
    }

    public function postCreate() {


    }

    public function getEdit($id, Config $config) { //injecting config model
        return view('edit')->with('config', $config->findOrFail($id));
    }

    public function postEdit($id, Config $config) {
        //do something with data that user sent us
        //Input::all();
        //$config->create([])


        //and redirect
        return Redirect::to('/');
    }

    public function destroy($id, Config $config) {
        $config->findOrFail($id)->delete();
        return Redirect::to('/');
    }

}

使用按钮生成“删除链接”

<a class="btn btn-small btn-warning" href="{{ route('config.delete', ['id' => $config->id]); }}">Delete</a>