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

grozavule's avatar

Capture path info from the request in the session

I want to capture each path that a user requests in the session. I thought that the best place to do this would be from the public/index.php file. Here is what I tried:

public/index.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
dump(session()->all());
if(session()->has('breadcrumb'))
{
    session('breadcrumb')->addPage($request->getPathInfo());
}
else
{
    dump("created breadcrumb");
    $breadcrumb = new App\DoubleL\Application\NavigationBreadcrumb();
    session(['breadcrumb' => $breadcrumb]);
    session('breadcrumb')->addPage($request->getPathInfo());
}
dd(session()->all());
$response->send();

$kernel->terminate($request, $response);

Here is the NavigationBreadcrumb class:

<?php

namespace App\DoubleL\Application;

class NavigationBreadcrumb
{
    protected $flow;

    public function __construct()
    {
        $this->flow = collect();
    }

    public function addPage($routeName)
    {
        $this->flow->push($routeName);
    }

    public function getPreviousPage()
    {
        return $this->flow->last();
    }
}

The session only ever stores the information for the most recent request. How can I make it save every path the user visits?

0 likes
2 replies
SilenceBringer's avatar

Hi @grozavule first of all, I think index.php is not the best place to do it. I think you can do Service Provider for this reason

And About your question

session('breadcrumb')->addPage($request->getPathInfo());

session('breadcrumbs') returns you NavigationBreadcrumb instance, and you correctly addPage, but you do nnot update session value. I think it should be something like

session(['breadcrumb' => session('breadcrumb')->addPage($request->getPathInfo())]);

Even whole your case

if(session()->has('breadcrumb'))
{
    session('breadcrumb')->addPage($request->getPathInfo());
}
else
{
    $breadcrumb = new App\DoubleL\Application\NavigationBreadcrumb();
    session(['breadcrumb' => $breadcrumb]);
    session('breadcrumb')->addPage($request->getPathInfo());
}

can be simplified to

session([
    'breadcrumb' => session('breadcrumb', new App\DoubleL\Application\NavigationBreadcrumb())->addPage($request->getPathInfo())]);

(just pass new NavigationBreadcrumb instance as default value if breadcrumbs do not exists in the session)

siangboon's avatar
Level 54

perhaps can write it as middleware and apply to every single route or group then within a route group

Please or to participate in this conversation.