- Module information
- Middleware configuration
- Nested folder routing
- Linking the public directory
- Directory locator
Composer module: squirrel-forge/lara-cs
Composer repository entry:
{
"type": "vcs",
"url": "git@github.com:squirrel-forge/composer.laravel.cs.git",
"no-api": true
}
php artisan vendor:publish --tag=sqf-cs
Example dotenv configuration, full list of values:
SQF_CS_USE=ip,range,keypass
SQF_CS_ENV=X-CLIENT-IP,REMOTE_ADDR
SQF_CS_IPS=000.000.000.000,0000:0000:0000:0000::0000:0000
SQF_CS_RANGES=000.000.000.00/00,0000:00:0000:0:0:0:0:0/00
SQF_CS_KEY=cxxa80quced65jg817xdxoalhhyn0blk
SQF_CS_PASS=g2czb0w289zl9xiom6kcy1nc0bbyfiao
SQF_CS_LIFETIME=Y-m-d-H
SQF_CS_LIMIT=60
SQF_CS_LOG=true
SQF_CS_CSP="default-src 'self'; script-src 'self' 'unsafe-inline'"
Check the configuration for detailed option descriptions.
Middleware that allows for runtime debug mode activation.
Middleware that sets global security headers for every response.
If your laravel runs in a domain root, but is requested via cdn url rewrite inside a nested path, Then follow these instructions to ensure all requests arrive where they are supposed to.
Prefix your routes with the nested path:
Route::group(['prefix' => '/prefixed/path/'], function () {
// ... prefixed routes
});
Set your asset path/url in your .env file:
ASSET_URL=/prefixed/path/
To allow all your public assets to be accessible even when requested from a virtual subdirectory, add following rules to your htaccess after the authorization header and before the trailing slash redirect.
# Rewrite routed asset paths
RewriteRule ^/prefixed/path/css/(.*)$ css/$2 [NC,QSA,L]
RewriteRule ^/prefixed/path/img/(.*)$ img/$2 [NC,QSA,L]
RewriteRule ^/prefixed/path/js/(.*)$ js/$2 [NC,QSA,L]
RewriteRule ^/prefixed/path/favicon.ico$ favicon.ico [NC,QSA,L]
RewriteRule ^/prefixed/path/robots.txt$ robots.txt [NC,QSA,L]
Or the recommended generic variant, which allows for dynamic prefixes:
# Rewrite routed asset paths
RewriteRule ^(.*)css/(.*)$ css/$2 [NC,QSA,L]
RewriteRule ^(.*)img/(.*)$ img/$2 [NC,QSA,L]
RewriteRule ^(.*)js/(.*)$ js/$2 [NC,QSA,L]
RewriteRule ^(.*)favicon.ico$ favicon.ico [NC,QSA,L]
RewriteRule ^(.*)robots.txt$ robots.txt [NC,QSA,L]
To copy or symlink your public directory to a new location, run the sqfcs:mvpub command. Note that running this command from the appropriate user/context will prevent permission issues.
The target can be relative to laravel root or system absolute. The --cp option defines, if and which files are copied instead of being linked. The option respects only file and folder names nested directly in the public dir, though folders will be copied recursively.
php artisan sqfcs:mvpub {target} {--cp=all|filename,dirname,...}
Any *.php files that are copied and not linked, will have "../" replaced with the new relative path to the laravel root.
If you must move/copy the public directory and not link it to another location, in your Kernels, set following code to let laravel know of the move:
use function SquirrelForge\Laravel\CoreSupport\joinAndResolvePaths;
/**
* Create a new HTTP kernel instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function __construct(Application $app, Router $router)
{
$app->usePublicPath(joinAndResolvePaths(base_path(), '../public/'));
parent::__construct($app, $router);
}
use function SquirrelForge\Laravel\CoreSupport\joinAndResolvePaths;
/**
* Create a new console kernel instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(Application $app, Dispatcher $events)
{
$app->usePublicPath(joinAndResolvePaths(base_path(), '../public/'));
parent::__construct($app, $events);
}
If you wish to use the directory locator service, you need to implement following code in your kernel constructors, to allow for setting the directories before laravel uses them.
use SquirrelForge\Laravel\CoreSupport\Service as SqfCs;
/**
* Create a new HTTP kernel instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function __construct(Application $app, Router $router)
{
SqfCs::locateEnvDir('env', $app);
SqfCs::locateStorageDir('cache', $app);
parent::__construct($app, $router);
}
use SquirrelForge\Laravel\CoreSupport\Service as SqfCs;
/**
* Create a new console kernel instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(Application $app, Dispatcher $events)
{
SqfCs::locateEnvDir('env', $app);
SqfCs::locateStorageDir('cache', $app);
parent::__construct($app, $events);
}