How can take locale from url in laravel? - laravel

I want to apply locale in an API, but it returns the ar language only. I want to take locale from url [ar or en] like : localhost/mertaah/api/en/settings it must take en locale but it take ar all the time
<?php
namespace App\Http\Middleware;
use Closure;
class ApplyAPILocale
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
*
* #return mixed
*/
public function handle($request, Closure $next)
{
app()->setLocale($request->header('locale') ?? 'ar');
return $next($request);
}
}

this is the solution that help me to solve the problem
https://laracasts.com/discuss/channels/laravel/pass-locale-to-url

Related

how to get RouteName in Laravel 5

I want to get current route Name that is being used in current url in middleware. i tried many example that nothing is working. please share best way to get that route name in Middleware.
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Closure;
class PermissionMiddleware {
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next) {
$user = Auth::user();
$pemissions = getUserPermissions($user);
session(['permissions' => $pemissions]);
$defaultPermission = $this->defaultPermission($user->user_type, $user->is_super);
$defaultPermission[] ='admin';
session(['defaultPermission' => $defaultPermission]);
return $next($request);
}
You can get route name from current request
$request->route()->getName()
or
request()->route()->getName()
$request->route()->getName()

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();
}

Database access in middleware Laravel 6

I would like to retrieve data from the database inside the middleware because I need to verify the token of which domain that can access to my system
you can see i have a file called VerifyCsrfToken.php that extend from Middleware so I want to get all domain that we have an inside table named domain
Simply user DB to make commands.
<?php
namespace App\Http\Middleware;
use DB;
use Closure;
class CheckAccessToken
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = DB::table('users')->first();
dd($user);
}
}
Or you can also use the model to access the DB
<?php
namespace App\Http\Middleware;
use Closure;
use App\User;
class CheckAccessToken
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = User::first();
dd($user);
}

Rewrite laravel nova middleware to response 404

I need to return 404 instead of 403 error page when regular user try to get access for admin pages.
The Nova middleware which is responsible for this is located here /nova/src/Http/Middleware.
And looks like this:
<?php
namespace Laravel\Nova\Http\Middleware;
use Laravel\Nova\Nova;
class Authorize
{
/**
* Handle the incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return \Illuminate\Http\Response
*/
public function handle($request, $next)
{
return Nova::check($request) ? $next($request) : abort(403);
}
}
If I change here abort(403) to abort(404) -
It works fine and doing exactly what I need.
How can I extend this middleware to use it in my application. What should I do to rewrite middleware properly, in my application, so that I can do updates of Nova in future and do not rewrite this changes
What I tried:
Extend this middleware in
<?php
namespace App\Http\Middleware;
use Laravel\Nova\Nova;
class NovaAuthorize extends \Laravel\Nova\Http\Middleware\Authorize
{
/**
* Handle the incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return \Illuminate\Http\Response
*/
public function handle($request, $next)
{
return Nova::check($request) ? $next($request) : abort(404);
}
}
and add it to my middlewares
protected $middleware = [
...
\App\Http\Middleware\NovaAuthorize::class, // nova access
];
but it had no effect
in /app/Providers/NovaServiceProvider.php
protected function gate()
{
Gate::define('viewNova', function ($user) {
if( !$user->isAdmin() ){
abort(404);
}
return true;
});
}

Lavary laravel menu not working

I am using Lavary's Laravel menu package for creating menus which is defined in middleware named frontMenu and applied it using route grouping.However when I access the particular route, it says Class 'App\Http\Middleware\Menu' not found.I have also correctly added content on config/app.php as per documentation.My middleware code is as follows:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Service\PageService;
class frontMenu
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
Menu::make('myNavBar', function($menu){
$menu->add('Home');
$menu->add('About', array('route' => 'page.about'));
$menu->about->add('Who are we?', 'who-we-are');
$menu->about->add('What we do?', 'what-we-do');
$menu->add('services', 'services');
$menu->add('Contact', 'contact');
});
return $next($request);
}
}
what have I done wrong ?
I was missing Use Menu;.Thanks #K.Toress for assist

Resources