How to inspect the current Named Route using Laravel 5.2 - php

according to the Laravel 5.5 docs, there is a named() method for accessing route names:
if ($request->route()->named('profile')) {
//
}
Inspecting the source, I learned that this named method simply fetches the 'as' property of the action object:
$this->action['as']
My problem is I'm stuck using Laravel 5.2, which doesn't have a named() method. I can't use route()->action['as'] in my blade template, because the actionobject is protected. Is there an equivalent getter method in 5.2, to check the name of the current route? I want to flow control in my blade.php file like this:
#if(route()->action['as'] == 'blog.edit')
//
#endif
Maybe I missed it, but I didn't see anything in the Laravel 5.2 docs: https://laravel.com/docs/5.2/routing#named-routes
I succeeded in checking the route using
#if(request()->is('blog/add'))
//
#endif
But that is using the route URI. I prefer to use the route name instead as it's less clunky

answer from Gitter courtesy of Ben Johnson:
#if(Route::currentRouteName() == 'blog.edit')
//
#endif

Related

Laravel 5.8 Adding Multiple Middleware Directly To Route

I'm using Laravel 5.8 and I have a route like this:
Route::get("certs","CertController#index")->name('certificate.front')->middleware('auth');
Now I wanted to add another middleware to this route, so I tried this:
Route::get("certs", "CertController#index")->name('certificate.front')->middleware('prevent-back-history','auth');
Now I don't get any error and it works But I wonder is this way better or not:
Route::get("certs", "CertController#index")->name('certificate.front')->middleware(['prevent-back-history','auth']);
So which is better and correct in this case?
Note that I don't want to use Route groups and needed to specify the middleware name directly to the route.
The last code is the correct way, because if you can see in your code
Route::get("certs", "CertController#index")->name('certificate.front')->middleware(['prevent-back-history','auth']);
If you wanted to have more than specific 1 middleware, you should use an array to define the middleware, so using [''] is the correct way, in case you wanted to add more middleware into the route.
#apose7523
Both the approaches are correct and are working, so it does not matter what approach you choose
Route::get('example',controller)->middleware('first', 'second');
Route::get('example',controller)->middleware(['first', 'second']);

What are some use cases of Named Routes in Laravel?

After reading the documentation, I still only have a vague idea of what Named Routes are in Laravel.
Could you help me understand?
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', 'UserProfileController#show')->name('profile');
It says:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function
I don't understand what the second part of the sentence means, about generating URLs or redirects.
What would be a generated URL in the case of profile from the above example? How would I use it?
The best resource is right here : https://laravel.com/docs/5.8/routing#named-routes
One of the common use case is in your views. Say your post request goes to a particular route, basically without named routes you can simply go like this to store a task
action="/task"
but say for example you need to update the route to /task/store , you will need to update it everywhere you use the route.
But consider you used a named route
Route::post('/task', 'TaskController#store')->name('task.store');
With named routes you can use the route like this in your view:
action="{{route('task.store')}}"
Now if you choose to update your route, you only need to make the change in the routes file and update it to whatever you need.
Route::post('/task/now/go/here', 'TaskController#store')->name('task.store');
If you need to pass arguments to your routes, you pass it as arguments to route helper like this:
route('task.edit', 1), // in resource specific example it will output /task/1/edit
All of the view examples are given you use blade templating.
After adding a name to a route, you can use the route() helper to create urls.
This can now be used in your application.
For instance, in your blade templates this may look like:
{{ route('profile') }}
This will use the application url and the route path to create a url.
this is how it looks it:
named route sample name('store');:
Route::get('/store-record','YourController#function')->name('store');
store is the named route here. to call it use route('store')
defining another type of route. this is not named route:
Route::get('/store-record','YourController#function')
you can access this route using {{ url('/store-record') }}
hope this helps

NotFoundHttpException in RouteCollection when routing with additional controller in laravel 5.2

I got this error-->'NotFoundHttpException in RouteCollection.php line 161'..When i try to call my additional controller in laravel 5.2..Already I did php artisan serve to activate localhost:8000..can you please explain the basic layout of routing with controller in laravel?
NotFoundHttpException occurs when no given route is matched to your given request to a certain endpoint/url.
Make sure you are sending the request to the correct url which is correctly defined in your routes.php (web.php for laravel 5.3+) with it's correct verb, (GET, POST, PATCH, etc).
Basic flow goes like this:
In your routes.php, you'd define a route like:
Route::get("/users", "UsersController#show");
then in your Http folder define that given controller with it's name which you referred in above call and anything proceeding # symbol is a callback function which gets called automatically.
So in your http/UsersController.php, you'd have:
public function show(Request $request) {
//Do something with your request.
return "Something"; //could be an array or string or
//whatever since laravel automatically casts it into JSON,
//but it's strongly recommended to use transformers and compact method.
}
For more information try looking at laravel docs, they provide an amazing way to get started tutorial. Laravel Docs

Get route pattern by route name in laravel 5.3

In laravel 5.1, I was able to get the route path by route name, for example:
Defined Route:
Route::post('users/{user_id}/delete', 'UserController#delete')->name('user:delete');
In laravel 5.1, when I tried the below method, It gave the route without any error If I didn't pass any route parameter:
route('user:delete'); // Output: http://example.com/users/%7Buser_id%7D/delete
and then in javascript, I simply replaced %7Buser_id%7D with the user id dynamically. But laravel 5.3 is throwing error when accessing route by name that has parameters and I don't want to pass one, because the parameters are set dynamically from the javascript.
Is there any way I can access route pattern by route name like:
http://example.com/users/{user_id}/delete
Or
/users/{user_id}/delete
Thanks in advance.
You can give some route method some value, that will be then replaced in javascript. For example: route('user:delete', 'USER_ID'), then in javascript you will simply replace USER_ID.
or the better way, is to use package called "Laroute"

Laravel 5.2 Auth with TwigBridge Session store not set

As mentioned in the title I'm using Laravel 5.2 and I've set up TwigBridge so I can use twig for my views. I'm just using the basic Auth package that comes with Laravel. The problem is when I use twig templates I get the following error:
An exception has been thrown during the rendering of a template ("Session store not set on request.") in "...resources/views/auth/register.twig" at line 17.
This is pointing to a use of the {{ old('name') }} call. I've tried switching it to input_old as I think TwigBridge prefers that, but that didn't help. If I use the blade template there's no problem though. I'm not doing anything special either. I just rename the blade template so it isn't called, and my register twig template is called instead since it uses the name register.
The old values are fetch from the session. To get these values to store in session & fetch these values back, you need to apply \Illuminate\Session\Middleware\StartSession and
\Illuminate\View\Middleware\ShareErrorsFromSession middlewares to your routes. Both of these are a part of middleware group web.
You can apply web middleware group to all your routes(unless you are creating an API), like this:
Route::group(['middleware' => ['web']], function () {
Route::get("login" , "AuthController#getLogin");
//Similarly all other routes here
});

Categories