Laravel Routes: Use ::controllers and ::post - php

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

Related

How to pass action to Laravel 8 routes?

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

Laravel Routing Parameter Contraint

Been working on a neat little project. I'm using Laravel 5.5, and I'm building routes to handle the various requests. I've got a route that accepts a slug to locate a particular guild via route model binding. Works great! Beautifully, actually. Then, I defined a "static" route that doesn't use a parameter to show the form for creating a new guild. Here's the routes...
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
But when I attempt to navigate to '/guild/create', I get a 404 because a guild with the slug "create" doesn't exist. How can I work around this particular issue?
Try put specific routes first:
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guil');

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 named routes parameter not working correctly

I'm currently using Laravel 5.3 and i have a number of routes similar to.
Route::get('/news/create/{product}', 'NewsController#create')->name('news::create');
So in my blade template im using the route() function like so:
{{route('news::create','car')}}
But the url generated is
/news/create?car
not the required
/news/create/car
The same thing happens if i put it in an array:
{{route('news::create',['car'])}}
And if i give it a key like so:
{{route('news::create',['product'=>'car'])}}
I get:
/news/create?product=car
How do I get the correct url so it is passed to the 'create' function as a parameter?
Firstly, take a look at your route naming. I don't think there's anything specifically wrong with naming a route like 'news::create' apart from it being ugly and quite probably considered bad practice. I like to go with camel casing, which means I'd use a name like createNews. It's much easier when going back to work on old sections of code and will stop other programmers from stabbing you if/when they work on a project with you.
The reason we can name routes is so that the name stays static even if we change the route URI or controller endpoint. We can pass variables to it using route parameters.
Route::get('/news/create/{product}', array('as' => 'createNews', 'uses' => 'NewsController#create'));
route('createNews', ['product' => 'car']);
{{route('news::create',['product => 'car'])}}
Should fix your problem. Laravel uses named routes and expects an array with the keys as names with values.
Read all about it here: https://laravel.com/docs/5.3/redirects#redirecting-named-routes

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

Categories