简体   繁体   中英

Setting session variables in Laravel 5.4

I have a feeling this is a very dumb question and there's probably something really tiny I just overlooked, but I'm stumped anyway.

Currently, I have an app with a few pages that are protected through middleware. If the user does not meet the requirements of these middleware, they are redirected to a login page. Now after they log in, I want them to be sent back to the page they tried to visit.

I've tried numerous things to accomplish this, but none work. What I'm trying to do now is the following:

  1. User attempts to access an admin page (for example /admin ): PagesController@adminDashboard
  2. When they access the overview() method in the controller (same as index(), but for admins), a session variable is set containing the url they tried to visit ( /admin )
  3. The user is redirected to the login page and logs in (SessionsController@create and @store)
  4. After logging in, they are redirected to the session variable with the intended URL

This is how I tried to do it:


PagesController

public function adminDashboard()
{
    $intended = '/admin';
    session('intended-url', $intended);

    //dd(session('intended-url');

    $schools = School::all();
    $articles = Article::all();
    $sights = Sight::all();

    return view('admin', compact('sights', 'articles', 'schools'));
}

SessionsController

public function store()
{
    /*
        ...
    */

    $intendedURL = session('intended-url');

    if($intendedURL != null)
    {
        return redirect($intendedURL);
    }
    else
    {
        return redirect()->home();
    }

Using dd() a few times here and there, I found out that it doesn't even set the session variable at the very start (commented dd() in PagesController returns null).

I've tried doing this using Session::put(), Session::set(), using square brackets as in session(['intended-url', '/admin']), but none of it gives me the result I'm looking for :(

Does anyone have any advice on how to do this, or perhaps a different way of accomplishing the same goal, but more efficiently? Thank you!

EDIT: I don't think the default Laravel redirect to intended page will work here, since I rewrote most of the login system from scratch to suit some specific needs. Unless anyone knows how that redirect works behind the scenes and I can over-/rewrite it

如果您使用的是默认身份验证系统,则可以使用此方法将用户重定向到他们要查看的页面:

return redirect()->intended('fallback/uri');

why u are not using middleware just make new middleware

php artisan make:middleware admin

and add this code of public function handle

if (!Auth::guest() && Auth::user()->admin) {

        return $next($request);
    }
     return redirect('/')->with('warning', 'This Area only For Admin');

add this code in kernal on protected $routeMiddleware

protected $routeMiddleware = [
'admin' => \App\Http\Middleware\admin::class,
];

make new column in users table

$table->boolean('admin')->default(false);

now you can use your construct function

 public function __construct()
{
    $this->middleware('admin');
}

Note:- where u add $this->middleware('admin'); all controller will be locked for users and guest only admin can use this controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM