GithubHelp home page GithubHelp logo

mahefaabel / lumen-session-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rummykhan/lumen-session-example

0.0 1.0 0.0 57 KB

Enable session in lumen framework (Laravel)

PHP 100.00%

lumen-session-example's Introduction

Lumen Session Example

Example of

https://stackoverflow.com/questions/47050984/enabling-session-in-lumen-framework/47055083#47055083

How-To

Laravel has officially stopped supporting sessions & views in laravel/lumen framework from version 5.2 and on wards.

But laravel still have a component illuminate/session which can be installed in lumen/framework and we can play around with this.

Step - 1

install illuminate/session using

composer require illuminate/session

Step - 2

Now goto bootstrap/app.php and add this middleware

$app->middleware([
    \Illuminate\Session\Middleware\StartSession::class,
]);

Purpose of adding the above middleware is to start session on every request and save session before serving response.

Step - 3

Now add config/session.php, since it is not present in Lumen by default. You can take session.php from Laravel official repo.

Step - 4

Create framework session storage directory by

mkdir -p storage/framework/sessions

Thanks to DayDream

Step - 5

In bootstrap/app.php add bindings for \Illuminate\Session\SessionManager

$app->singleton(Illuminate\Session\SessionManager::class, function () use ($app) {
    return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session');
});

$app->singleton('session.store', function () use ($app) {
    return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session.store');
});

Thanks to @xxRockOnxx for finding loadComponent method. It takes 3 arguments,

  • first one is config file name. (file should be present in config/ directory)
  • second is ServiceProvider FQN
  • third is return of this method.

loadComponent just calls the $app->register and inject $app while building the ServiceProvider

How to Use

// Save Session
$router->get('/', function (\Illuminate\Http\Request $request) {

    $request->session()->put('name', 'Lumen-Session');

    return response()->json([
        'session.name' => $request->session()->get('name')
    ]);
});


// Test session
$router->get('/session', function (\Illuminate\Http\Request $request) {

    return response()->json([
        'session.name' => $request->session()->get('name'),
    ]);
});

How to use this example

You can clone this project using

git clone [email protected]:rummykhan/lumen-session-example.git 

cp .env.example .env

composer install

php  artisan serve

Contact

[email protected]

lumen-session-example's People

Contributors

rummykhan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.