Php 为什么我被重定向到显示而不是破坏路线
当我尝试注销时,我从Php 为什么我被重定向到显示而不是破坏路线,php,laravel,laravel-4,routing,Php,Laravel,Laravel 4,Routing,当我尝试注销时,我从show路径进入函数,而不是destroy路径 这是视图中的注销按钮: @if (Auth::check()) <li> {{ HTML::linkRoute('account.destroy', 'Logout', Auth::user()->id) }} </li> @endif 以及显示和销毁功能: public function show($id) { $user = User::find($id)->fi
show
路径进入函数,而不是destroy
路径
这是视图中的注销按钮:
@if (Auth::check())
<li>
{{ HTML::linkRoute('account.destroy', 'Logout', Auth::user()->id) }}
</li>
@endif
以及显示
和销毁
功能:
public function show($id)
{
$user = User::find($id)->first();
return View::make('account.visit_profile')->with('user', $user);
}
public function destroy($id)
{
Auth::logout();
Session::flush();
return Redirect::route('home');
}
如果我将show
放在资源
路径的例外中,并为注销创建一条路径:
Route::get('/account/logout', [
'as' => 'account.destroy',
'uses' => 'AccountController@destroy'
]);
然后我仍然被重定向到show
功能
我不明白为什么会这样。对于一个资源丰富的控制器,为
show
路由和destroy
路由生成的URL是相同的,唯一的区别是用于访问它们的HTTP谓词。使用GET请求访问show
方法,使用DELETE请求访问destroy
方法。这两种方法相当于:
Route::get('account/{account}', ['as' => 'account.show', 'uses' => 'AccountController@show']);
Route::delete('account/{account}', ['as' => 'account.destroy', 'uses' => 'AccountController@destroy']);
HTML锚定标记只能执行GET请求,因此无法通过HTML锚定标记访问destroy
操作。该链接将指向“account/{account}”url,但它将使用GET请求,从而将您带到show
方法
基本解决方案是,您需要一个表单来提交POST请求,而不是HTML链接,该表单包含一个隐藏字段“\u method”,其值为“DELETE”。这里有几个选项
@if (Auth::check())
<li>
{{ Form::open(['route' => ['account.destroy', Auth::user()->id], 'method' => 'delete']) }}
{{ Form::submit('Logout') }}
{{ Form::close() }}
</li>
@endif
@if(Auth::check())
{{Form::open(['route'=>['account.destroy',Auth::user()->id],'method'=>'delete'])}
{{Form::submit('Logout')}
{{Form::close()}}
@恩迪夫
如果需要,可以使用CSS使表单/提交按钮看起来像一个链接@if (Auth::check())
<li>
{{ HTML::linkRoute('account.destroy', 'Logout', Auth::user()->id, ['data-method' => 'DELETE']) }}
</li>
@endif
@if(Auth::check())
{{HTML::linkRoute('account.destroy','Logout',Auth::user()->id,['data-method'=>'DELETE']}
@恩迪夫
这就是你所需要的。javascript将处理其余部分@if (Auth::check())
<li>
{{ HTML::linkRoute('account.destroy', 'Logout', Auth::user()->id, ['data-method' => 'DELETE']) }}
</li>
@endif