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