[英]Laravel | how to change route if user is not authenticated
I don't know If this is possible with laravel or what is the best way to achieve it, i wan't if user click to like post or add to favorites and he is not authenticated to showing login form, 我不知道laravel是否可以实现这一目标,或者最好的方法是什么,我不希望用户单击喜欢的帖子或将其添加到收藏夹,并且他没有通过身份验证即可显示登录表单,
I want every visitor to have access to all posts, i have used before if(auth::check())
in my view to hide or display the buttons (like & favorite) if user authenticated, but i don't like this way. 我希望每个访问者都可以访问所有帖子,如果用户通过身份验证,我曾在
if(auth::check())
之前使用过if(auth::check())
来隐藏或显示按钮(如&最喜欢的),但我不喜欢这种方式。 i want to display the buttons to all visitors but when a Unauthenticated user click on like button redirecting to login page. 我想向所有访问者显示按钮,但是当未经身份验证的用户单击像按钮一样重定向到登录页面时。 I tried to add this method to my routes but seems not working
我试图将此方法添加到我的路线中,但似乎不起作用
if (Auth:check()){
Route::post('/favorite/{post}', 'SiteController@favorite');
Route::post('/unfavorite/{post}', 'SiteController@unFavorite');
Route::post('/like/{post}', 'SiteController@like');
Route::post('/update/{post}', 'SiteController@update');
Route::post('/unlike/{post}', 'SiteController@unLike');
} else {
Route::get('/login', ['as' => 'login', 'uses' => 'Auth\LoginController@showLoginForm']);
You are supposed to use middlewares for things like this. 您应该将中间件用于此类事情。 Group the routes you want only logged in users to have access to and use a middleware on that route to check if the user is authenticated.
将您只希望登录的用户有权访问的路由进行分组,并在该路由上使用中间件来检查用户是否已通过身份验证。
Example: web.php
示例:
web.php
<?php
// Routes for logged in users.
$router->group(['middleware' => ['auth']], function($router) {
$router->get('foobar', ['as' => 'foobar', 'uses' => 'FooController@index']);
});
// Routes for all users.
$router->get('bar', ['as' => 'bar', 'uses' => 'BarController@index']);
App\\Http\\Middleware\\Authenticate.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest(route('bar'));
}
}
view()->share('user', Auth::user());
return $next($request);
}
}
Then in App\\Http\\Kernel.php
you add this to your $routeMiddleware
array: 然后在
App\\Http\\Kernel.php
,将其添加到$routeMiddleware
数组中:
'auth' => \App\Http\Middleware\Authenticate::class,
To make the like button visible you have to remove the auth::check())
in your views and add this to your web.php
: 要使like按钮可见,您必须在视图中删除
auth::check())
并将其添加到web.php
:
// Routes for guest users
// Add here the route for your posts
// Routes for logged-in users
Route::group(['middleware' => ['auth']], function () {
Route::post('/favorite/{post}', 'SiteController@favorite');
Route::post('/unfavorite/{post}', 'SiteController@unFavorite');
Route::post('/like/{post}', 'SiteController@like');
Route::post('/update/{post}', 'SiteController@update');
Route::post('/unlike/{post}', 'SiteController@unLike');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.