fabian
fabian

Reputation: 5463

Error 500: How to get session variable via Laravel custom config file?

I want to use a custom config file to access a session variable – or a fallback if not set.

But I get an HTTP ERROR 500 once i add Session::get()

[05-May-2016 19:53:16] PHP Deprecated: Non-static method Symfony\Component\HttpFoundation\Session\Session::has() should not be called statically in /Users/.../config/constants.php

What's wrong?

<?php
// File: app/config/constants.php

use Symfony\Component\HttpFoundation\Session\Session;


$something = Session::has('something') ? Session::get('something') : "fallback";


return [
    'something ' => $something
];

Update 1:

use Session;

results in this error:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist' in /Users/…/vendor/laravel/framework/src/Illuminate/Container/Container.php:738 Stack trace: #0 /Users/…/vendor/laravel/framework/src/Illuminate/Container/Container.php(738): ReflectionClass->__construct('log') #1 /Users/…/vendor/laravel/framework/src/Illuminate/Container/Container.php(633): Illuminate\Container\Container->build('log', Array) #2 /Users/…/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(697): Illuminate\Container\Container->make('log', Array) #3 /Users/…/vendor/laravel/framework/src/Illuminate/Container/Container.php(853): Illuminate\Foundation\Application->make('Psr\Log\LoggerI...') #4 /Users/…/vendor/laravel/framework/src/Illum in /Users/…/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 738

Upvotes: 2

Views: 1742

Answers (2)

Jameel Ahmed Ansari
Jameel Ahmed Ansari

Reputation: 71

You can't access session in config file in laravel the alternative solutions is to store your session variable in a cookie and then access that cookie in config file. Use $_COOKIE php array to access cookies.

Upvotes: 0

Ohgodwhy
Ohgodwhy

Reputation: 50767

You should be useing the Facade from the Illuminate\Support package.

use Illuminate\Support\Facades\Session;

Then you can correctly use Session::

Upvotes: 3

Related Questions