Laravel not changing language - laravel

Laravel is not changing the language I have tried these methods in controller
if ($request->lang === 'English') {
config(['app.locale' => 'en']);
} else {
config(['app.locale' => 'ar']);
}
and this method
App::setLocale('ar')
Or this method
\App::setLocale('ar')
What should I do?

You can create a middleware that puts the locale in the session and sets it.
php artisan make:middleware SetLocale
app\Http\Middleware\SetLocale.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class SetLocale
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($request->input('lang') == 'English') {
$request->session()->put('locale', 'en');
} else {
$request->session()->put('locale', 'ar');
}
App::setLocale($request->session()->get('locale'));
return $next($request);
}
}
Then, add it to your global middleware (or to a middleware group).
app\Http\Kernel.php
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* #var array
*/
protected $middleware = [
// other global middlewares
\App\Http\Middleware\SetLocale::class,
];

Related

Laravel redirection to https

Hi i ve an app made with laravel and this address http://example.com/check/.
I wanted to redirect to https so i created this middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\App;
class HttpsProtocol
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (!$request->secure()) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
}
}
Then i added
\App\Http\Middleware\HttpsProtocol::class
to Kernel in App/Http in protected $middlewareGroups
Redirection seems to work but redirect to this address https://example.com/check/check
with repeated URI (check)
Why
Thx a lot
It looks like you don't need to append the request URI, that is happening already so try:
if (!$request->secure()) {
return redirect()->secure();
}

Assigning middleware inside a Middleware handle function in Laravel

I want to know If it is possible to assign a middleware or execute a middleware in handle function of another middleware
<?php
namespace App\Http\Middleware;
use Closure;
class AuthTypeMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if($request->header('auth_type') === "api") {
// execute the auth:api middleware here
}
else {
// execute the auth middleware here
}
}
}
I know we can achieve this by creating different route groups with prefix but still I wanted to know if their any possibility of getting this work

error "Target class [Wazawaza2Middleware] does not exist."

I want to use middleware in laravel but show that.
enter image description here
I think my code is right.
Wazawaza2Middleware.php
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;
class Wazawaza2Middleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check()){
return $next($request);
}else{
return view('auth.login');
}
}
}
web.php
use App\Http\Middleware\Wazawaza2Middleware;
Route::get('topde', 'ReviewController#top')->middleware('Wazawaza2Middleware::class');
Kernel.php
protected $routeMiddleware = [
.
.
.
'wazawaza2' =>
\App\Http\Middleware\Wazawaza2Middleware::class,
];
You have an error in your web.php, should be:
use App\Http\Middleware\Wazawaza2Middleware;
Route::get('topde', 'ReviewController#top')->middleware(Wazawaza2Middleware::class);
OR (since you are aliasing it)
Route::get('topde', 'ReviewController#top')->middleware('wazawaza2');

Laravel Middleware Class 'App\Http\Middleware\CheckAuth' not found

So I'm trying to use middleware to authenticate users on a few pages of my application, but I'm getting this error:
Class 'App\Http\Middleware\CheckAuth' not found
Here's CheckAuth.php:
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use App\Http\Middleware\CheckAuth as Middleware;
class CheckAuth extends Middleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::user()->auth > 0) {
return redirect()->route('dashboard');
}
return $next($request);
}
}
and here's Kernel.php:
protected $routeMiddleware = [
...
'authenticated' => \App\Http\Middleware\CheckAuth::class
];
When I try to use the middleware (like this ->middleware('authenticated');) I get the error.
Thanks.
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class CheckAuth
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::user()->auth > 0) {
return redirect()->route('dashboard');
}
return $next($request);
}
}
Remove use App\Http\Middleware\CheckAuth as Middleware; you're in the same Class File. you don't need to use it again.
Remove this line from the top of middleware.
use App\Http\Middleware\CheckAuth as Middleware;
And you don't need to extends that Middleware as well
Now, your code looks like below.
namespace App\Http\Middleware;
use Closure;
use Auth;
class CheckAuth
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::user()->auth > 0) {
return redirect()->route('dashboard');
}
return $next($request);
}
}
Use command to create middleware
php artisan make:middleware CheckAuth

Return every request as plain json Laravel using Middleware

I have a route in my web.php that returns a view:
Route::get('/', function () {
return view('welcome');
});
welcome is default Laravel view welcome.blade.php.
I have Middleware called AlwaysReturnJson and it contains:
<?php
namespace App\Http\Middleware;
use Closure;
class AlwaysReturnJson
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
I set up this middleware in Kernel.php as global middleware:
protected $middleware = [
\App\Http\Middleware\AlwaysReturnJson::class
];
What I expect is to get plain text/json of welcome file in my browser when I navigate to given route but I always get it as html and it render page properly. I checked it, it applies middleware on every request so that is not a problem. Why is this happening and shouldn't it convert that view to a plain text? Am I doing something wrong?
If you want to set a header for your response you can do this:
namespace App\Http\Middleware;
use Closure;
class AlwaysReturnJson
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
If you want to force return valid json content use this middleware instead:
namespace App\Http\Middleware;
use Closure;
class AlwaysReturnJson
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
return response()->json($response->getContent());
}
}
See Laravel docs about after middleware for more info.
You can alternatively return json response on your controller without any middleware needed:
Route::get('/', function () {
return response()->json(
view('welcome')->render()
);
});
You may want to use laravel After middleware (the middleware would perform its task after the request is handled by the application) and then set the content-type of response.
<?php
namespace App\Http\Middleware;
use Closure;
class AfterAlwaysReturnJson
{
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->header('Content-Type', 'application/json');
}
}

Resources