Asked  9 Months ago    Answers:  5   Viewed   177 times

Is there any way available to access Session values in AppServiceProvider? I would like to share session value globally in all views.



You can't read session directly from a service provider: in Laravel the session is handled by StartSession middleware that executes after all the service providers boot phase

If you want to share a session variable with all view, you can use a view composer from your service provider:

public function boot()
    view()->composer('*', function ($view) 
        $view->with('your_var', Session::get('var') );    

The callback passed as the second argument to the composer will be called when the view will be rendered, so the StartSession will be already executed at that point

Wednesday, March 31, 2021
answered 9 Months ago

While your users are accessing via HTTPS, your CloudFlare configuration is passing those requests onto your server using HTTP. You have two options:

  1. Turn on CloudFlare's Full or Strict SSL.

  2. Set up your webserver to see the X-Forwarded-Proto header and tell PHP it's being accessed via HTTPS when that header's value is https. How to do this depends a bit on your webserver. In nginx, for example, I do this for Amazon's ELB (which sends the same headers):

map $http_x_forwarded_proto $is_https {
    default off;
    https on;

and then this in my FastCGI params file:

fastcgi_param  HTTPS              $is_https if_not_empty;

In Apache, I think you can do this if mod_setenvif is installed:

SetEnvIf x-forwarded-proto https HTTPS=on
Wednesday, March 31, 2021
answered 9 Months ago

The URL method is used to get URL for the file (ie: something to access it from the browser). What you want to use id the get() method.

$contents = Storage::get(''margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'');

That being said, there is still easier way to manipulate uploaded files with Laravel.

// this will take the uploaded file from the request
// and store it into `/storage/someFolder`
$path = $request->file('file')->store('someFolder');

You can then use the value returned to $path to access the file.


After discussion below, it was decided by OP that, even if it's not the the perfect solution, he would use $objPHPExcel = PHPExcel_IOFactory::load('..'.Storage::disk('local')->url($fileLocation));.

Saturday, May 29, 2021
answered 7 Months ago

The best approach is to create a symbolic link like @SlateEntropy very well pointed out in the answer below. To help with this, since version 5.3, Laravel includes a command which makes this incredibly easy to do:

php artisan storage:link

That creates a symlink from public/storage to storage/app/public for you and that's all there is to it. Now any file in /storage/app/public can be accessed via a link like:

If, for any reason, your can't create symbolic links (maybe you're on shared hosting, etc.) or you want to protect some files behind some access control logic, there is the alternative of having a special route that reads and serves the image. For example a simple closure route like this:

Route::get('storage/{filename}', function ($filename)
    $path = storage_path('public/' . $filename);

    if (!File::exists($path)) {

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;

You can now access your files just as you would if you had a symlink:

If you're using the Intervention Image Library you can use its built in response method to make things more succinct:

Route::get('storage/{filename}', function ($filename)
    return Image::make(storage_path('public/' . $filename))->response();


Keep in mind that by manually serving the files you're incurring a performance penalty, because you're going through the entire Laravel request lifecycle in order to read and send the file contents, which is considerably slower than having the HTTP server handle it.

Tuesday, June 1, 2021
answered 7 Months ago

The controller is not the right place to check if a user is authenticated or not. You should use a middleware for that. To get info on what a middleware is check here

Let's see how you can use the default Laravel's auth middleware for this purpose:

First of all get rid of your AdminBaseController and use only AdminController

Then you have to check that the auth middleware is enabled in the file appHttpKernel.php

You should have the line:

protected $routeMiddleware = [
    'auth' => AppHttpMiddlewareAuthenticate::class,

This means that the middleware is active and usable for your routes.

Now let's go inside the middleware class in appHttpMiddlewareAuthenticate.php to specify the middleware's behaviour :

//this method will be triggered before your controller constructor
public function handle($request, Closure $next)
    //check here if the user is authenticated
    if ( ! $this->auth->user() )
        // here you should redirect to login 

    return $next($request);

Now the only thing left to do is to decide for what routes you should apply the middleware. Let's suppose you have two routes that you want to be only accessible from authenticated users, you should specify to use the middleware for these two routes in this way:

Route::group( ['middleware' => 'auth' ], function()
    Route::get('admin/index', 'AdminController@index');
    Route::get('admin/ajuda', 'AdminController@ajuda');
Monday, September 27, 2021
Angshuman Agarwal
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :