Jishad P
Jishad P

Reputation: 703

Laravel Redirect when Middleware Process checking

I am developing a laravel web application of business directory.

here is my scenario.

  1. http://localhost/genie-works/devojp/customer // user searching with keyword of business and

  2. Showing results in http://localhost/genie-works/devojp/customer/search-result?keyword=apple&searchcity=1 this page.

  3. here listing too many business data with post an enquiry feature.

  4. when clicking on the post an enquiry button page goes to http://localhost/genie-works/devojp/customer/post-enquiry/{bisinjessid}/

  5. the post enquiry page checking a middle-ware as authentication.

  6. when user not logged in the middleware redirect to login page http://localhost/genie-works/devojp/customer and showing the login form

  7. after entering login details its needs to redirect to http://localhost/genie-works/devojp/customer/post-enquiry/{bisinjessid}/ this page.

  8. but i tried the function Redirect::back its redirecting to customers page (http://localhost/genie-works/devojp/customer)

    How can i solve this issue by redirecting to my last page....

Thanks

Middleware..

 public function handle($request, Closure $next)
 {
    if (!Auth::check()) {
        return redirect()->intended(route('cust_index'))->with('activelogin','Succesfully LoggedOut !!!');
        }
    return $next($request);
 }

Controller..

public function custdologin(){
    $userdata=array(
    'username'=>Input::get('email'),   // getting data from form
    'password'=>Input::get('password')   // getting data from form
    );
    if(Auth::attempt($userdata)){
        switch (Auth::user()->user_type) {
            case '2':

                return Redirect::to(route('myaccount'));
                break;
            case '3':
                return back();
                break;
            default:
                Auth::logout();
                return Redirect::to(route('business_login'))->with('message','Check Your Entries!!');
                break;
        }
    }   
    else
    return Redirect::to(route('business_login'))->with('message','Check Your Entries!!');
}

Upvotes: 1

Views: 186

Answers (1)

The Alpha
The Alpha

Reputation: 146191

In your middleware where you are using redirect, use the following:

return redirect()->intended('put a default url'); // i.e: '/dashboard'

This will redirect the user to the intended url (s)he wanted to go without being logged in. Check more here (in Manual Authentication code snippet)

Upvotes: 1

Related Questions