Laravel Route using array - php

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

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

How to do a simple redirect in Laravel?

I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?
I tried something like:
return redirect()->route('listofclubs');
It doesn't work.
The route for "listofclubs" is:
Route::get("listofclubs","Clubs#listofclubs");
If you want to use the route path you need to use the to method:
return redirect()->to('listofclubs');
If you want to use the route method you need to pass a route name, which means you need to add a name to the route definition. So if modify your route to have a name like so:
// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs#listofclubs']);
Then you can use:
return redirect()->route('listofclubs');
You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs#listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use
return redirect()->action('Clubs#listofclubs');
Then in route file
Route::get("listofclubs","Clubs#listofclubs");
Laravel will automatically redirect to /listofclubs.

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

Route definition not working in Laravel

I have 2 routes in my routes file.
Route::get('/deals/{merchant_name}?c={deal_id}', ['uses' => 'dealsvisibleController#index']);
Route::get('/deals/{merchant_name}', ['uses' =>'dealsController#index']);
Both routes are calling on a different controller function. The first route is however not working.
I am trying this in a 3rd controller.
return redirect('deals/'.$merchant_name.'?c='.$deal_id);
However, when the page redirects, it is calling dealsController#index and not dealsvisibleController#index
Can someone help me with why this is happening.
Laravel's router considers only path when matching URLs to your routes. Therefore, if you redirect to deals/someMerchant?c=someDealId then it uses deals/someMerchant to match the URL.
You'll need to define the first route as deals/{merchant_name}/{deal_id} in order for this routing to work as you want it to.

Handling generated urls in laravel?

I'm making a link shortener as part of a school project,
Route::get('{short_url}', array('uses' => 'UrlController#shortUrlRedirect');
This function works fine alone, but as I have other functions such as
Route::post('register', array('uses' => 'HomeController#doRegister'));
whenever example.com/anylink
is now used, it is handled by one function alone.
A working solution I found would be to do something like:
Route::get('url/{short_url}', array('uses' => 'UrlController#shortUrlRedirect');
But of course with a link shortener, the goal is to have as little characters as possible.
Any ideas of a possible way to handle this issue within laravel?
The earlier or 'higher' in the routes.php file is the route, the more priority it gets, so if you define two identical routes or two routes that match one pattern, like in your example, the first one will be executed.
So you should define register route earlier, as it should not be overriden by the {short_url}.
Here is the explanation: Routes: First in, first out
TL;DR: Laravel receives a request, and uses the URI of the request to find a matching pattern iterating the routes file, when it finds one, it break;s the loop.

Categories