Is Laravel introducing new programming syntax? - laravel

What is this Closure $next. Ok I am guessing an object but how a object reference accepts an argument $request? Shouldn’t it call method. Closure in php are anonymous function but is it that the next is an anonymous function variable defined else where? I see the handle method is in Class Frameguard but the above example didn’t even instantiate Frameguard Class. Am I correct to say Laravel does not follow oop syntax?
public function handle($request, Closure $next)
{
if ($request->age <= 200) {
return redirect('home');
}
return $next($request);
}

Now, it does not. Laravel is just framework so it allows you to complete some every-day actions in easier and faster way.
Obviously Laravel follows OOP and there is no something like "OOP syntax" as far as I know. But there are parts in Laravel and every other framework that are not so obvious.
Basically in middleware you have access to Request and closure. You can read about middleware here. If you want to dig in dipper you can look at \Illuminate\Pipeline\Pipeline class - there is carry method in there that is responsible for looping over each used middleware.

Related

How to let a unit test ignore a middleware in Laravel [duplicate]

This question already has answers here:
How to Disable Selected Middleware in Laravel Tests
(6 answers)
Closed 3 years ago.
I'm testing an endpoint in my Laravel app. However, I have a middleware that does complex logic to determine the location of the user (using ip reverse look up etc such as this code:
public function getOpCityByIP()
{
// Get the client's remote ip address
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
$clientIpAddress = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
} else {
$clientIpAddress = $_SERVER['REMOTE_ADDR'];
}
$opCityArray = OpCityIP::get($clientIpAddress);
return $opCityArray;
}
I'm not interested in going inside the guts of such methods in said middleware and having to mock them etc. I'd rather simply skip the entire middleware during the unit testing, or at least mock its entire operations and instead hardcode the result to something predetermined. How do I do that?
I'm using Laravel 5.4
update
i need it to ignore a specific middleware, not all of them
You can use the withoutMiddleware() method on your test object. As of Laravel 5.5, this method accepts a parameter that lets you specify which middleware to disable, instead of just disabling them all.
In your test:
$this->withoutMiddleware([YourGeoIpMiddleware::class]);
To solve my problem of skipping a specific middleware, I simply put this code inside the handle method of said middleware:
public function handle($request, Closure $next)
{
if (config('app.env') === 'testing') {
return $next($request);
}
..
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
class ExampleTest extends TestCase
{
use WithoutMiddleware;
// Stuff
}

What is better practice for accessing laravel request variables

What is better practice for accessing request values in laravel.
For example I have method update() in laravel controller.
I want to get values from request, which is better way:
method(Request $request) {
$request->inputName;
}
or
method() {
request('inputName');
}
Is it better to create request instance as method attribute or to use Laravel helper method request().
Thanks
Both are the same, the first approach might be better in case you create a custom form request where you do the validation of the form. Other than that both provide the same thing.
In your Controller
use Illuminate\Http\Request;
public function update(Request $request)
{
$name = $request->input('name');
}
I think this is best way to use
method(Request $request) {
$request->inputName;
}
Even laravel documentation suggest this.
laravel provides you with methods to get values from the request
now I have request variable is $request, it is instance of Request
The better practice for accessing laravel request variables:
If GET method, you should use $request->get('variable_name')
If POST method, you should use $request->input('variable_name')
if you want check request has variable you can use $request->has('variable_name')
Good luck

What does 'return $next($request)' do in Laravel middleware?

Please respect that I'm new to programming and Laravel, so this question might seem a little odd to the most of you.
But I think this is what stackoverflow is for, so:
When I created a new middleware with the command php artisan make:middleware setLocale there was already the handle-function with this code in it:
return $next($request);
and I'm wondering what exactly this line does.
$next($request) just passes the request to next handler.
Suppose you added a middleware for checking age limit.
public function handle($request, Closure $next)
{
if ($request->age <= 18) {
return redirect('home');
}
return $next($request);
}
when age is less than 18 it will redirect to home but when the request passes the condition what should be done with the request? it will pass it to next handler.Probably to the register user method or any view.
This is explained in the documentation:
To pass the request deeper into the application (allowing the middleware to "pass"), call the $next callback with the $request.
It's best to envision middleware as a series of "layers" HTTP requests must pass through before they hit your application. Each layer can examine the request and even reject it entirely.
https://laravel.com/docs/5.8/middleware#defining-middleware

Laravel Routes and 'Class#Method' notation - how to pass parameters in URL to method

I am new to Laravel so am uncertain of the notation. The Laravel documentation shows you can pass a parameter this way:
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
Which is very intuitive. Here is some existing coding in a project I'm undertaking, found in routes.php:
Route::get('geolocate', 'Api\CountriesController#geolocate');
# which of course will call geolocate() in that class
And here is the code I want to pass a variable to:
Route::get('feed/{identifier}', 'Api\FeedController#feed');
The question being, how do I pass $identifier to the class as:
feed($identifier)
Thanks!
Also one further sequitir question from this, how would I notate {identifier} so that it is optional, i.e. simply /feed/ would match this route?
You should first create a link which looks like:
/feed/123
Then, in your controller the method would look like this:
feed($identifier)
{
// Do something with $identifier
}
Laravel is smart enough to map router parameters to controller method arguments, which is nice!
Alternatively, you could use the Request object to return the identifier value like this:
feed()
{
Request::get('identifier');
}
Both methods have their merits, I'd personally use the first example for grabbing one or two router parameters and the second example for when I need to do more complicated things.

Laravel 5.0 pass variable to middleware

At the moment I have to check if job record, which is being edited, belongs to right person. My get job edit route:
/user/job-edit/{slug}
So I created JobEditMiddleware but the problems is I can't access {slug} variable in my middlewar. Is there any way to do it? Thanks.
You can use segment() method to retrieve various segments of your URI.
Try following in your middleware,
\Request::segment(3)
Read More
You can access to your slug parameter easier.
public function handle($request, Closure $next, $role) {
//
}
You have to call your slug parameter like this :
$request->slug;
I think it's a better way than segment if you'll need to change your route later.

Resources