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
Related
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,
];
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();
}
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');
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);
}
I have trouble with redirecting to an url after login.
The situation is that someone visits a blog post, and needs to login before adding a comment. So the user clicks on the login link and logs in on "auth/login", and is always redirected to "/home".
I want the user to be redirected to the blogpost when an url is set like "auth/login?redirect=url/to/blogpost"
I have the following Middleware:
app\Http\Middleware\RedirectIfAuthenticated
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class RedirectIfAuthenticated
{
/**
* The Guard implementation.
*
* #var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* #param Guard $auth
* #return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->check()) {
return redirect('/home');
}
return $next($request);
}
}
app\Http\Middleware\Authenticate
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class Authenticate
{
/**
* The Guard implementation.
*
* #var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* #param Guard $auth
* #return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
}
}
return $next($request);
}
}
Why don't you use the intended method on redirector? Read about this in docs
The intended method on the redirector will redirect the user to the URL they were attempting to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.
I've decided to copy and paste the getLogin function of the trait AuthenticatesUsers into my AuthController. I overwrite the function AND keep the trait as is.
I've just added
\Session::put('url.intended',\URL::previous());
If you're using standard authentication from Laravel 5, find a app/Http/Controllers/Auth/AuthController.php file and change $redirectPath to this:
protected $redirectPath = '/url/to/blogpost';