简体   繁体   中英

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

You should be use ing the Facade from the Illuminate\\Support package.

use Illuminate\Support\Facades\Session;

Then you can correctly use Session::

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM