Laravel路由中间件验证
我的路线是这样的:Laravel路由中间件验证,laravel,Laravel,我的路线是这样的: Route::get('free-download/{product}' , 'ProductController@freeDownload')->name('product.free')->middleware('auth'); 此路由在调用freeDownload方法之前检查用户是否已登录。如果没有,则显示登录表单 然后用户需要登录,登录控制器返回主页,用户需要再次单击按钮route('product.free')以访问路由名称“product.free”
Route::get('free-download/{product}' , 'ProductController@freeDownload')->name('product.free')->middleware('auth');
此路由在调用freeDownload方法之前检查用户是否已登录。如果没有,则显示登录表单
然后用户需要登录,登录控制器返回主页,用户需要再次单击按钮route('product.free')以访问路由名称“product.free”
有办法打电话给你ProductController@freeDownload方法公正
登录后,用户是否在之前单击了按钮
希望我或多或少是清楚的
这里是我的登录控制器:
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
最后这里是我的免费下载方法
public function freeDownload(Product $product){
$user_download = UserDownload::where('user_id' , auth()->user()->id)->where('product_id' , $product->id)->exists();
if(!$user_download && $product->file){
$user_download = new UserDownload();
$user_download->user_id = auth()->user()->id;
$user_download->product_id = $product->id;
$user_download->save();
$product->downloads = $product->downloads + 1;
$product->save();
return Storage::disk('s3')->download($product->file);
}else{
Alert::error('Download error', 'file not found');
return back();
}
}
在登录控制器中,使用
预定的
方法
public function login(Request $request)
{
if ($this->guard()->attempt(...)) {
return redirect()->intended(route('home'));
} else {...}
}
在被中间件捕获之前,它将尝试重定向到预期页面。如果找不到所需的页面,它将重定向到
主页
页面。感谢您的回复,只有在登录前我请求路由时,此方法才会将我重定向到路由“product.free”?我应该在尝试中放置什么?是的,尝试
是您放置凭据以登录用户的地方。检查您当前的AuthControllerit不工作,请将我带回家中,但不要发送(“product.free”)。顺便说一下,这个按钮在我的主视图中。我没提这个。