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');
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,
];
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 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');
}
}
Hello i am learning laravel for first time on topic controllers.I need to get this output
First Middleware,
Second Middleware,
URI: usercontroller/path,
URL: http://localhost:8000/usercontroller/path,
Method: GET
My Following codes are:
UserControler.php
namespace FirstProject\Http\Controllers;
use Illuminate\Http\Request;
use FirstProject\Http\Requests;
use FirstProject\Http\Controllers\Controller;
class UserController extends Controller
{
public function _construct(){
$this->middleware('auth');
}
}
FirstMiddleware.php
namespace FirstProject\Http\Middleware;
use Closure;
class FirstMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
echo "<br>First Middleware";
return $next($request);
}
}
SecondMiddleware.php
namespace FirstProject\Http\Middleware;
use Closure;
class SecondMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
echo "<br>Second Middleware";
return $next($request);
}
}
SecondUserController.php
namespace FirstProject\Http\Controllers;
use Illuminate\Http\Request;
use FirstProject\Http\Requests;
use FirstProject\Http\Controllers\Controller;
class SecondUserController extends Controller
{
public function __construct(){
$this->middleware('Second');
}
public function showPath(Request $request){
$uri = $request->path();
echo '<br>URI: '.$uri;
$url = $request->url();
echo '<br>';
echo 'URL: '.$url;
$method = $request->method();
echo '<br>';
echo 'Method: '.$method;
}
}
Routes/web.php
Route::get('/usercontroller/path',[
'middleware' => 'First',
'uses' => 'UserController#showPath'
]);
But When im running http://localhost:8000/usercontroller/path
I m getting
BadMethodCallException
Method [showPath] does not exist on [FirstProject\Http\Controllers\UserController].
What is the problem?
It is quite obvious, isn't it ? This method is defined in SecondUserController but not in UserController and in routes you use 'UserController#showPath'
I am using laravel5.2 and i followed https://laravel.com/docs/5.3/middleware as per that i created MyMiddleware.php in Middleware folder
Here is code.
<?php
namespace App\Http\Middleware;
use Auth;
use Closure;
class MyMiddleware {
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string $role
* #return mixed
*/
public function handle($request, Closure $next, $role) {
echo $role;exit;
return $next($request);
}
}
In kernel.php:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\MyMiddleware::class
]
];
protected $routeMiddleware = [
'absurd' => \App\Http\Middleware\MyMiddleware::class,
];
In routes.php :
Route::any('manager/dashboard', 'UserController#mndashboard')->middleware('absurd:Admin');
But I still get Error: ErrorException in MyMiddleware.php line 18: Missing argument 3 for App\Http\Middleware\MyMiddleware::handle()
I tried everything but not working.
Help Need Plz .!
Add default value to your handle method:
public function handle($request, Closure $next, $role='default_vale') {
echo $role;exit;
return $next($request);
}
Update:
First remove the middleware from web group. Then add it to your route group as bellow
Route::group(['middleware' => ['web', 'absurd:admin']], function(){
//Your Routes
}