drnycqxwz63508434 2018-01-21 11:48
浏览 98

Laravel | 如果用户未经过身份验证,如何更改路由

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,

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. 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']);
  • 写回答

2条回答 默认 最新

  • dotj78335 2018-01-21 11:52
    关注

    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

    <?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:

    'auth' => \App\Http\Middleware\Authenticate::class,
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数