Laravel 5.3+ don’t load .env file

It was s strange situation… My JS and CSS files are versioned using an own method. In the asset() links a config value is included, which comes from the .env file, but after changing the needed value in the .env, nothing happened, the old file was loaded, instead of the new one…
So I’ve started to search for the reason.

My asset link looks like the following:

1
<link rel="stylesheet" href="{{ asset('/assets/css/occupancy_editor_v' . config('app.AssetFileVersion') . '.css') }}"/>

My .htaccess have one rewrite role:

1
2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^(.+)_v\d\d\d+\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$" "$1.$2" [L]

This ensures, that the _vxxx string will be rewritten to the original file name, but upon change of the file, the browser won’t load it from cache, because it gets the link with the _vxxx suffix…
In the _vxxx suffix, the xxx part comes from the .env file.

Checked the .htaccess, it was okay. Cleared the browser cache, nothing…

After some examination made on the dotenv library, it comes out, that values containing space required to put between ” signs. I’ve also had values containing ‘/’ sign, and it is a good thing to put them between “”-s too.

ArrowChat integration to Laravel 5.3+

Recently I’ve bought Arrowchat (https://www.arrowchat.com) for one of my customers. During the installation of arrowchat, you have to set up some users table values like:

  • Table name
  • user id field name
  • user name field name

This works correctly, but Arrowchat can not read the logged in user id, because usually Laravel encrypts the cookies, just like session cookie.

To solve this, we should bootstrap Laravel in the integration file of Arrowchat.

So the start of the integration.php should be start like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$base_path = dirname(dirname(dirname(dirname(__FILE__))));
require_once($base_path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
require_once($base_path . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'autoload.php');
$app = require_once($base_path . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'app.php');
 
try {
    $app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture());
 
    $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
 
     $app['session']->driver()->setId($id);
     $app['session']->driver()->start();
 
} catch(\Exception $ex) {
}

 

This bootstraps the Laravel app. But to receive the correct user id from Laravel we should rewrite the get_user_id function to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function get_user_id() {
    $userid = NULL;
    global $app;
 
    $app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture());
 
    $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
    $app['session']->driver()->setId($id);
    $app['session']->driver()->start();
 
    if($app['auth']->user()!= NULL){
         $userid = $app['auth']->user()->id;
    }
 
    return $userid;
}

And that’s it! Arrowchat now can read the user id from the session, and can display logged in users correctly!