I keep getting an error every time I use myroute middleware. Below is the middleware class. its currently empty.
<?php
namespace App\Http\Middleware;
use Closure;
class EnforceHttps
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
}
When I attach it to my route like so it gives me an error.
Route::get('/', ['middleware' =>'https',function () {
//Route::get('/', function () {
return view('launch');
}]);
If I uncomment the line below which does not include the middleware instead all is good. Why is this?
Related
I'm trying to run some middleware on my routes via my package's service provider before the routes run in my Laravel project but I'm not seeing my var_dump despite running php artisan route:list showing that the BeforeMiddleware is applied. Am I using the wrong middleware?
My service provider:
<?php
namespace Company\FudgeInboundManagementBridge;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Company\FudgeInboundManagementBridge\Http\Middleware\BeforeMiddleware;
class FudgeInboundManagementBridgeServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
$this->registerFiles();
}
/**
* Register files
*/
protected function registerFiles(): void
{
Route::middleware(BeforeMiddleware::class)->group(function () {
$this->loadRoutesFrom(__DIR__.'/routes/fudge-routes.php');
});
$this->publishes([
__DIR__ . '/config/FudgeInboundManagementBridge.php' => config_path('FudgeInboundManagementBridge.php'),
], 'config');
}
}
My middleware:
<?php
namespace Company\FudgeInboundManagementBridge\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class BeforeMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle(Request $request, Closure $next)
{
var_dump('hello from before middleware - bridge');
die;
// return $next($request);
}
}
Seems like it's not triggering?
Route::middleware('admin')->group(function () {
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard');
Route::get('profile', 'AdminController#profile')->name('profile');
Route::post('profile', 'AdminController#profileUpdate')->name('profile.update');
Route::get('password', 'AdminController#password')->name('password');
Route::post('password', 'AdminController#passwordUpdate')->name('password.update');
I have this code in routes and I want to add "if username == "staff"
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard');
not others pages.
First create StaffMiddleware.php in following path app/Http/Middleware/StaffMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
class StaffMiddleware
{
/**
* Handle an incoming request. User must be logged in to do admin check
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (\Auth::user()->username == 'staff')
{
return $next($request);
}
return redirect()->guest('/');
}
}
In app/Http/Kernel.php add the following line
protected $routeMiddleware = [
// your existing code
'staff' => \App\Http\Middleware\StaffMiddleware::class,
];
Rewrite the following route
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard');
TO
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard')->middleware('staff');
Now whenever you want to check this condition you just need to add ->middleware('staff') in route.
i used from modular structure in laravel that all module in Module folder that routes registerd in ModuleService provider with:
$this->loadRoutesFrom(__DIR__.'/../Routes/web.php');
when i not use from middleware('web') in route then csrf token return null and when i use from this middleware then when user not logined and try to show dashboard page then show error:
Call to a member function setCookie() on null in middleware
Route
Route::get('account/dashboard', [StaffController::class, 'dashboard'])
->middleware(['web','staff'])->name('staff-dashboard');
middleware:staff
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class staff
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle(Request $request, Closure $next)
{
if(Auth::guard('staff')->check() == false)
{
return view('staffauth::login');
}
return $next($request);
}
}
how to resolve this problem?
I use Request::path() in \routes\web.php as follow and visit '/home/test/test1' worked well, i.e., the path is shown in the website.
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home/test/test1', function(){
echo Request::path();
});
However, when I did it in \app\Http\Middleware, the error "Class 'App\Http\Middleware\Request' not found" showed up.
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAuth
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$path = Request::path();
return $next($request);
}
}
You missed in your Middleware file:
use Illuminate\Http\Request;
Change your Middleware code to:
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;
class CheckAuth
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle(Request $request, Closure $next)
{
$path = $request->path();
return $next($request);
}
}
That should fix it.
Reference: https://laravel.com/docs/5.8/requests#request-path-and-method
Actually you missed to add the reference in the middleware file. So add use Illuminate\Http\Request; at below the namespace. Then Error should be fixed.
I'm trying to make middleware role for my authentication in my project, this is my middleware called sales.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Sales
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if($request->user()->role == "sales"){
return $next($request);
}
return redirect()->route('login')->with('error',"You don't have an access");
}
}
and this is the route i protect from middleware
Route::get('dashboard','DashboardController#index')->middleware('sales')->name('dashboard');
and when i try to access dashboard to make sure my route is protected by middleware, it shows error like this
Trying to get property of non-object
i appreciate if you answer this !
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Sales
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check() && Auth::User()->role=='sales'){
return $next($request);
}
return redirect()->route('login')->with('error',"You don't have an access");
}
}