Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nathanrobjohn's avatar

Help with Laravel Redirect::back()

Hi Guys,

I picked up a project that needed a huge refactor as everything is currently inside the controllers and models.

I moved the checkout login process with the db to a repo here is my controller

<?php

namespace App\Http\Controllers;

use DB;
use App\Http\Controllers\Controller;
use App\User;
use App\Wishlist;
use App\Order;
use App\Place;
use App\Common;
use App\Http\Requests;
use App\Http\Requests\CheckoutLoginRequest;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\RedirectResponse;
use Validator;
use Input;
use Auth;
use Illuminate\Support\Facades\Hash;
use Session;
use Gloudemans\Shoppingcart\Facades\Cart;
use App\Repositories\UserRepositoryInterface;
use Mail;

class UsersController extends Controller
{

    public function __construct(UserRepositoryInterface $users)
    {
        $this->users = $users;
        session_start();
    }
   

    /**
     * Login via checkout with basket
     * @param CheckoutLoginRequest $request
     */
    public function checkoutLogin(CheckoutLoginRequest $request)
    {
        $this->users->checkoutLogin($request);
    }


}

and here is my user repo

<?php

namespace App\Repositories\Eloquent;

use App\User;
use App\Http\Requests;
use Auth;
use App\Repositories\UserRepositoryInterface;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Hash;
use Session;
use Gloudemans\Shoppingcart\Facades\Cart;

class UserRepository implements UserRepositoryInterface {

    /**
     * Create a new UserRepository instance.
     *
     * @param  \App\User  $user
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get current user.
     *
     */
    public function currentUser()
    {
        $currentUser = Auth::user();
        return $currentUser;
    }

    /**
     * Find all users
     * @return $users
     */
    public function findAll()
    {
        return $this->user->orderBy('created_at', 'desc');
    }


    /**
     * Find all users paginated.
     *
     * @param int $perPage
     * @return $users
     */
    public function findAllPaginated($perPage = 10)
    {
        return $this->user->orderBy('created_at', 'desc')->paginate($perPage);
    }

    /**
     * Register user
     *
     * @param $request
     */
    public function register($request)
    {

    }

    /**
     * Login via checkout with basket
     * @param $request
     * @return View
     */
    public function checkoutLogin($request)
    {

        if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha']) {

            Session::flash('login', 'function');
            return Redirect::back()->withErrors("Invalid captcha");

        } else {

            $user = $this->user->where('email', '=', $request['email'])->first();

            if( Hash::check($request['password'], $user->Password) ){
                    Auth::login($user);
                    $user = Auth::user();
                    Session::put('PersonName', $user->FirstName.' '.$user->MiddleName.' '.$user->LastName);
                    Session::put('PersonID', $user->PersonID);
                    Session::put('Email', $user->Email);
                    Session::put('MobilePhone', $user->MobilePhone);

                    $carts = Cart::content();

                    foreach($carts as $cart)
                    {
                    }
                    if(!empty($cart))
                    {
                        //Todo Fix this (not redirecting back properly)
                        return Redirect::back();
                    }
                    else
                    {
                        return redirect('/');
                    }
            } else {

                Session::flash('login', 'function');
                return Redirect::back()->withErrors(array('loginerror' => 'The account entered has not been found.'));

            }
        }

    }

    /**
     * Get User by id
     * @param $id
     * @return $user
     */
    public function getUserById($id) {
        return $this->user->where('PersonID','=',$id)->first();
    }


    /**
     * Delete User by id
     * @param $id
     * @return redirect
     */
    public function deleteUserById($id) {
        return $this->user->where('PersonID','=',$id)->delete();
    }

    /**
     * Get User by Email
     * @param $email
     * @return $user
     */
    public function getUserByEmail($email) {
        return $this->user->where('Email','=',$email)->first();
    }

    /**
     * Logout user
     */
    public function logoutUser() {
        Auth::logout();
        Session::flush();
    }

} 

Inside the function checkoutLogin once they have logged in and if something is in the cart i am trying to redirect back but for some reason i just get a blank white page if i dump and die Redirect::back() i get

RedirectResponse {#288 ▼
  #request: Request {#37 ▼
    #json: null
    #sessionStore: null
    #userResolver: Closure {#227 ▶}
    #routeResolver: Closure {#245 ▶}
    +attributes: ParameterBag {#39 ▶}
    +request: ParameterBag {#38 ▶}
    +query: ParameterBag {#45 ▶}
    +server: ServerBag {#42 ▶}
    +files: FileBag {#41 ▶}
    +cookies: ParameterBag {#40 ▶}
    +headers: HeaderBag {#43 ▶}
    #content: "_token=QdcPNtsmCk5kmXisA4nYYrN8EffY7t97qdH6yk4n&email=nathanrobjohn%40gmail.com&password=&captcha=venomed"
    #languages: null
    #charsets: null
    #encodings: null
    #acceptableContentTypes: null
    #pathInfo: "/checkoutlogin"
    #requestUri: "/aerobus/public/checkoutlogin"
    #baseUrl: "/aerobus/public"
    #basePath: null
    #method: "POST"
    #format: null
    #session: Store {#205 ▶}
    #locale: null
    #defaultLocale: "en"
  }
  #session: Store {#205 ▶}
  #targetUrl: "http://localhost:812/aerobus/public/makepayment"
  +headers: ResponseHeaderBag {#287 ▶}
  #content: """
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <meta http-equiv="refresh" content="1;url=http://localhost:812/aerobus/public/makepayment" />
    
            <title>Redirecting to http://localhost:812/aerobus/public/makepayment</title>
        </head>
        <body>
            Redirecting to <a href="http://localhost:812/aerobus/public/makepayment">http://localhost:812/aerobus/public/makepayment</a>.
        </body>
    </html>"""
  #version: "1.0"
  #statusCode: 302
  #statusText: "Found"
  #charset: null
}
0 likes
6 replies
pmall's avatar

You have to return something from the controller action. Here you are returning nothing.

1 like
phildawson's avatar
    public function checkoutLogin(CheckoutLoginRequest $request)
    {
        return $this->users->checkoutLogin($request);
    }

Edit: ninja'd by pmall again :(

1 like
nathanrobjohn's avatar

omg donut sorry have been up all night trying to clear everything up! thank you guys! my fault.

jekinney's avatar

Fyi if your using 5.0 or greater you can use return back().

bashy's avatar

Why native $_SESSION? And you can do what @jekinney said!

return back(); // same as return redirect()->back();
nathanrobjohn's avatar

@bashy most of the code above is not mine and i am currently just extracting everything from the controllers into their rightful place but will be going through everything. but thanks for tip bash

Please or to participate in this conversation.