Passing variable to middleware in laravel 5.x - php

I would like to send a VARIABLE to my middleware in laravel ,the way I tried is this
Route::get('projects/{id}','projectsController#index')->middleware(['auth', 'project:{id}']);
But all I get in the middleware handle function is '{id}' where as I want the value of the id variable
public function handle($request, Closure $next,$pid)
{
dd($pid);
return $next($request);
}
I read the documentation on middleware for the version of laravel I am using i.e. 5.2 https://laravel.com/docs/5.2/middleware
But the example given there only talks about passing plain strings as parameters

I figured it out , looks like you don't need to pass query pararmets like this, you can simply pull them using the $request variable like so :
public function handle($request, Closure $next)
{
$pid=$request->id;
return $next($request);
}

Related

I want to make a custom middleware in laravel but i have not idea how to supply token input

"I want to make a custom middleware in laravel but I have not idea how to supply token input.
Here I want to match token input with my secret key.
The code is:"
This is snapshot of my code
If you use this middleware for API, you can pass secret key in header at every request and function handle in EnsureTokenIsValid should look like this
public function handle(Request $request, Closure $next){
if ($request->header('secret_key') !== 'your-secret-key'){
// logic here
}
return $next($request);
}
If you use it for normal http requests pass it in body or query params
public function handle(Request $request, Closure $next){
if ($request->get('secret_key') !== 'your-secret-key'){
// logic here
}
return $next($request);
}
Hope help u and happy coding !

Laravel share variable to views in custom middleware group

I need to put $page variable into views in some group of routes. I found one solution to put routes in middleware group and in middleware use View::share. But if I place any function in middleware will this code run for
How does view composer work? Does it try to run function on each view? I'm just thinking about performance if one page is from multiple views....
Middleware:
public function handle(Request $request, Closure $next)
{
View::share('page', Page::get());
return $next($request);
}
Will query for getting Pages get called once for multiple views?

Middleware not working as expected - Laravel

This is driving me crazy as I think I'm doing the right thing but its not working correctly.
I have a route with a middleware attached to it like below;
Route::get('post/{id}/{name}', 'BlogController#post')->name('blog-post')->middleware('blogGuard');
As you can see I've defined 2 route params
In my controller I have below;
public function post () {
return view('pages.blog.post');
}
With the middleware defined like this;
public function handle($request, Closure $next)
{
if (is_null($request->input('id')) ||
is_null($request->input('name'))) {
return redirect()->route('blog-home');
}
return $next($request);
}
Now if I click on a link like so; http://blog.example.co.uk/post/153/firstpost the middleware should not fire correct?
This is not the case. The middleware executes and I'm redirected. But if I remove the middleware then I'm able to access the page.
Any help appreciated.
If you are trying to access route parameters you probably want to explicitly get them from the route.
$request->route('id'); // pulls the $route->parameter('id');
$request->route('name'); // pulls the $route->parameter('name');
$request->id will check the request inputs before falling back to returning a route parameter.
$request->input('id') will only check the input sources for the request and not the route params.
If you use $request->id expecting to get the route param 'id', one could break your logic by passing id=anythinghere to the querystring or adding a 'id' var to a post request.
Try this
if (!$request->id ||
!$request->name)

Laravel 5 - Getting URL Parameters in Middleware on Resources

Let's say that I have a resource defined in my Routes as:
Route::resource('account', 'AccountController', ['only'=> ['index','update']]);
And then I have the Middleware attached to the Controller from within as:
public function __construct() {
$this->middleware('BeforeAccount', ['only' => ['update']]);
}
Let's say I want to access the uri parameter that happens after account (i.e. example.com/account/2) within my Middleware - how do I go about grabbing that variable?
You can use the following code to achieve that:
public function handle($request, Closure $next)
{
$account_id = $request->route()->parameter('accounts');
//...
}
Since the handle method receives the Request object as the first argument. The middleware gets executed only after the route has been matched so the Request object contains the current route and no need to match the route again using Route::getRoutes()->match($request).
Doing this way you don't have to supply the \Request object:
Route::current()->parameter('parameter');

Lumen: how can I get url parameters from middleware

This is my routes.php:
$app->get('/users/{id}/', ['middleware' => 'example', function () {
return "users";
}]);
This is the handle function in the middleware:
public function handle($request, Closure $next)
{
// I would like to get the value of the url parameter {id} here
return $next($request);
}
Is there a way I can get the parameter id from my middleware?
* Edit *
I'm using Lumen 5.1.0.
There are some conventional ways in Laravel doesn't work on Lumen. And get parameter form URI in middleware is one of them. In Laravel, I just need to call $request->id, it will work like magic. But here in order to get parameter in Lumen, I need to do something like this:
$request->route()[2]['id']
If the $request value passed in is an instance of Illuminate\Http\Request, which I think it might be, that class has a method called input(), which lets you do exactly that:
You should try this:
$id = $request->input('id');
i think the latest lumen 5.6 official tutorial about middleware is no longer applicable, and out of date.

Categories