Laravel is a PHP framework, in the use of Laravel will encounter session use problems, in the use of the process encountered some problems and some examples of application. Use Laravel to develop the application, copy the original code, the previous code SESSION used $_session, this thought that the transplant can be very good run, because there is no reliance on other components, the results appear this undefined variable: _ SESSION
Configuration files for the Laravel session are configured in the app/config/session.php, you can see the options settings and comments available in the session configuration file when you use them. Laravel uses file to implement the session by default. She does not use PHP native $_session (PHP native session to see the location of php.ini), so ignore the PHP related session functions, such as session_start (), $_session. Laravel will be in the app during operation/storage/session/directory to write information about the session, so this directory needs to have write permission, no session will not be able to write successfully. Laravel, in addition to using the default file as the session implementation, also supports cookies, Memcached, Redis, and database backend drivers as a session implementation. When necessary, you also need to implement a session, such as in the public account and user interaction, the session can not be used directly, because each time the server to request, not through the source of the request to identify the user.
Laravel's session Brief API
Session API is relatively simple, we look at the Chinese document also probably know how to mean. But there are a few that are not very well understood. //permanent Save of Session (within the non-expiring range)Session::p ut ('Key','value'); //native session equivalent to PHP$_session['Key'] ='value'; //Get Operation$value = Session::Get('Key','default'); //Remove operation and Delete, similar to pop concept$value = Session::p ull ('Key','default'); //detect if a key existsSession::has ('Users'); //Delete keySession::forget ('Key'this correspondence, as long as the session is not period, is basically permanently saved, the next HTTP request is also present. Different from the flash concept below. Laravel in the session Flash concept but laravel out a quick flash concept, I got mixed up all of a sudden. This flash two requests are valid (this time and the next request is valid), regardless of how many times this request takes action. //Save Key,valueSession::flash ('Key','value'); //The value method is still the same.Session::Get('Key'); //refreshes the flash data time and remains to the next requestSession::keep (Array ('username','Email'); This flash concept is not the same as the concept of put above.
Put: This corresponds as long as the session is not period, basically is permanently saved, the next request also exists. Flash: Saved values, this request can be used, the next HTTP request can be used, and the next one will not exist. That is, the next time the request is destroyed, will not let the value of the session become more and more large, you can save some temporary data.
This scenario uses scenarios such as: a user requests a page, an error message appears, redirects to a new page, and the previous data needs to be displayed. (although it can be passed by URL parameters, it may have an XSS vulnerability if handled poorly).
The user visited a page, the filter found no permissions, save the current page URL, redirect to the login page, login success, remove the value, redirect to the original page. (You may need to refresh the saved flash data here)
When the session landed I naively thought that the session was used::p ut function can save this variable. So my code writes like this:classLogincontroller { Publicfunction Login () {Session::p ut ('Key','value'); Print_r (Session::all ()); //Take a look to see if put succeedsExit//habitual debug All exit, no subsequent code execution//return redirect::to (/); The framework will have subsequent code execution after return . }}
Results the next request is not to find this session, and see the app/storage/The Session Directory is no file generation. There's always something wrong.
Later saw on the network there is a method Session::save (), so I also used the next, incredibly found a successful generation of the session of the file. So I feel that, laravel do not have PHP native session, then after the controller should do something, the session is written to the file, not every put operation is write operation, so that the IO operation is too frequent, affecting performance.
View the code associated with the call. After compiling laravel, the bootstrap/in compiled.phpclassMiddleware implements httpkernelinterface{ ... Publicfunction handle (Request $request, $type = Httpkernelinterface::master_request, $Catch=true) { $ This-checkrequestforarraysessions ($request); if($ This-sessionconfigured ()) {$session= $ This->startsession ($request);//Start Session$requestsetsession ($session); } $response= $ This->app->handle ($request, $type, $Catch);//call the controller's method if($ This-sessionconfigured ()) { $ This->closesession ($session);//Close Session$ This-addcookietoresponse ($response, $session); } return$response; } ... protectedfunction closesession (sessioninterface $session) {$session->save ();//Save Session$ This-CollectGarbage ($session); }} Tip: If you do not know the function call situation, you can throw in the controllerNewException ();, and then debug in/config/app.php to Debug=>true. You can see the call relationship of the function.
You can see that the session is called after the controller has been called->save () method, to actively save the session. So that the session can be ground to save, if the controller or view to write the exit;
Then the session will not be saved, unless the active write Session::save () can be manually saved. So in debug debugging when you must pay attention to AH.
Laravel Framework Session using tutorial