for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\Ship\Features\Middlewares\Http;
use Closure;
use Illuminate\Foundation\Application;
/**
* Class Localization
*
* @author Mahmoud Zalt <[email protected]>
*/
class Localization
{
* Localization constructor.
* @param \Illuminate\Foundation\Application $app
public function __construct(Application $app)
$this->app = $app;
app
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
}
* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
public function handle($request, Closure $next)
// read the language from the request header
$locale = $request->header('Content-Language');
// if the header is missed
if(!$locale){
// take the default local language
$locale = $this->app->config->get('app.locale');
// check the languages defined is supported
if (!array_key_exists($locale, $this->app->config->get('hello.supported_languages'))) {
// respond with error
return abort(403, 'Language not supported.');
// set the local language
$this->app->setLocale($locale);
// get the response after the request is done
$response = $next($request);
// set Content Languages header in the response
$response->headers->set('Content-Language', $locale);
// return the response
return $response;
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: