How to pass action to Laravel 8 routes? - php

I was using following Route declaration to pass language action to the routes.
Route::get('/de', ['uses' => 'PageController#showHomepage', 'language' => 'de']);
And in controller, I was able to get this value like this:
$action = $request->route()->getAction();
// $action['language'] gives the given value here..
But with callable syntax, I couldn't find how can I do the same thing. When using the following type of route definition:
Route::get('/', [PageController::class, 'showHomepage']);
How can I pass the language to new declaration? Is that a way to pass arguments to callable in Route::get() ?
Something like this
Route::get('/', ([PageController::class, 'showHomepage'],'languagecode_parameter_here'));
Thank you very much.
PS: If you are asking why do I need such custom parameter, here is more details about the need: https://github.com/laravel/framework/discussions/44839

Reading your last comment, I would suggest you to create a middleware to set the language. You can call your route with the middleware that is actually "right" for it (or group the routes with that middleware). This way, your project is more future-orientated and organized.
See the docs https://laravel.com/docs/master/middleware

You can’t.
And this is not appropirate way to localize your routes in laravel.
If you want to set localization for your laravarl application there is a package fir that and the name of pakcage in github is mcamara/laravel-localization it is one of the best localization packages.
Package github link

Related

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

Laravel Route using array

Route::resource('posts','PostsController');
Route::post('posts/changeStatus', array('as' => 'changeStatus', 'uses' => 'PostsController#changeStatus'));
The code provided is a route from my laravel project. I did not write this code and I am attempting to understand what they have done. I cannot find anywhere in the documentation the reason for using the key value store with 'as' and 'uses'. I would normally write the code below, however this is not working with the ajax-crud setup.
Route::post('posts/changeStatus', 'PostsController#changeStatus');
From the docs:
Named routes allow you to conveniently generate URLs or redirects for a specific route. You may specify a name for a route using the as array key when defining the route
as is the name of that route. You can use it to create an URL with route('changeStatus') helper.
uses is controller method (action) for the route.
https://laravel.com/docs/5.1/routing#named-routes

Laravel named routing | Auto generating urls to best possible match

I am trying to name my routes so I can easily generate urls instead of using the url helper function in which I have to type myself.
I am doing this:
Route::get('logout', 'PageController#logout')->name('pc');
When I do php artisan route:list I don't get this named route, instead name column is empty.
What I want is that, whatever views I target through this controller. I want to generate their urls like this.
route('pc.logout') OR route('pc.some_other_action')
So even if I change the verb to access this controller from the URL address, I don't have to change my url generator. Is this even possible in Laravel? Please also tell how to do this for resource routes.
I am using Laravel 5.3.
Thanks.
EDIT: Next day 1/11/2017
What I am expecting is that like ASP.NET MVC routing, if I tell it to generate a url with given controller and actions, it will generate the possible route to it regardless of the verb or string used to name the route.
For example
If I make a url like this
route('users.index')
Outputs /users
but if I change the string name users to u, instead of giving me error that name 'users' doesn't exist it should auto generate a url output like this:
Output: /u
This is a part of ASP.NET MVC routing system, I am expecting to see this here as well.
You should write this This will solve your problem
Route::get('logout', array('as' =>'pc.logout' ,'uses' => 'PageController#logout'));
This might solve your problem
you can define the named route as this
Route::get('/logout',[
'uses'=>'web\PageController#logout',
'as'=>'pc'
]);
Try this:
<?php
Route::get('logout',[
'uses'=>'PageController#logout',
'as'=>'pc.logout'
]);
name('pc.logout') OR name('pc.some_other_action')
You can try this one.
It is much simpler way to use:
Route::get('logout', 'PageController#logout')->name('pc.logout');
Or like this:
Route::get('logout', 'PageController#logout')->name('pc.some_other_action');
can you explain further if this is wrong.

Laravel 5 redirect to path with parameters (not route name)

I've been reading everywhere but couldn't find a way to redirect and include parameters in the redirection.
This method is for flash messages only so I can't use this.
return redirect('user/login')->with('message', 'Login Failed');
This method is only for routes with aliases my routes.php doesn't currently use an alias.
return redirect()->route('profile', [1]);
Question 1
Is there a way to use the path without defining the route aliases?
return redirect('schools/edit', compact($id));
When I use this approach I get this error
InvalidArgumentException with message 'The HTTP status code "0" is not valid.'
I have this under my routes:
Route::get('schools/edit/{id}', 'SchoolController#edit');
Edit
Based on the documentation the 2nd parameter is used for http status code which is why I'm getting the error above. I thought it worked like the URL facade wherein URL::to('schools/edit', [$school->id]) works fine.
Question 2
What is the best way to approach this (without using route aliases)? Should I redirect to Controller action instead? Personally I don't like this approach seems too long for me.
I also don't like using aliases because I've already used paths in my entire application and I'm concerned it might affect the existing paths if I add an alias? No?
redirect("schools/edit/$id");
or (if you prefer)
redirect("schools/edit/{$id}");
Just build the path needed.
'Naming' routes isn't going to change any URI's. It will allow you to internally reference a route via its name as opposed to having to use paths everywhere.
Did you watch the class Illuminate\Routing\Redirector?
You can use:
public function route($route, $parameters = [], $status = 302, $headers = [])
It depends on the route you created. If you create in your app\Http\Routes.php like this:
get('schools/edit/{id}', 'SchoolController#edit');
then you can create the route by:
redirect()->action('SchoolController#edit', compact('id'));
If you want to use the route() method you need to name your route:
get('schools/edit/{id}', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
// based on CRUD it would be:
get('schools/{id}/edit', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
This is pretty basic.
PS. If your schools controller is a resource (CRUD) based you can create a resource() and it will create the basic routes:
Route::resource('schools', 'SchoolController');
// or
$router->resource('schools', 'SchoolController');
PS. Don't forget to watch in artisan the routes you created

Laravel Routes: Use ::controllers and ::post

Within Laravel, I currently have:
Route::controllers([
'bla' => 'BlaController',
]);
But for a few specific methods within BlaController, I would like to pass parameters via the route (eg: /bla/parameter) and then be able to access that parameter within the controller.
However, declaring:
Route::get('bla/{parameter}', 'BlaController#exampleMethod');
keeps giving me a 404 not found when visiting it.
Is there some kind of conflict between using both declarations within routes.php and if so, is there a way to pass parameters to the controller for specific methods?
Edit: This is Laravel 5.1, if that makes any difference!
Can u try this ?
Route::get('/bla/{parameter}' ['uses'=>'BlaController#exampleMethod']);
http://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller

Categories