Route definition not working in Laravel - php

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.

Related

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 Routing to Wrong View

I'm working with Laravel 4.2. I have an app that's routing to the wrong view, although the URL is correct. On a button click, it's supposed to route to users.create (UsersController#create), but is instead routing to UsersController#show. The resolved URL is correct, though, and the DOM element has the correct URL listed. Can anyone help me out?
Here is my Routes file:
// Home page
Route::get('/', 'BaseController#index');
// Define User model to pass through routes
Route::model('user', 'User');
// Create custom route for editing a user
Route::get('users/edit/{user}',
array('as' => 'users.edit', 'uses' => 'UsersController#edit'));
// Create custom route for showing a user
Route::get('users/{user}',
['as' => 'users.show', 'uses' => 'UsersController#show']);
// Remaining routes
Route::resource('users', 'UsersController',
array('except' => array('edit', 'show')));
Here is my UsersController with the two functions in question:
class UsersController extends \BaseController {
protected $user;
public function create()
{
return View::make('users/create');
}
public function show($user)
{
return View::make('users/show', ['user' => $user]);
}}
And here are the relevant results from php artisan routes:
GET:HEAD users/{user} users.show UsersController#show
GET:HEAD users/create users.create UsersController#create
Thanks for your help!
Edit:
The answer to the problem was to simply re-order the routes so that the resource is defined first. I was aware that Laravel grabs the first route that matches a URI, but I still don't understand why a route that isn't passed a user object would select a route defined as users/{user}. Furthermore, I was accessing the route via link_to_route(), that is to say, by name. Why would Laravel pick a different route from the one I explicitly named?
I suppose these questions are beyond the scope of the initial question, but I would greatly appreciate further explanation from someone. Problem solved!
The first thing that jumps out at me is there is no route for "create". There is the "restful" controller, but maybe you want to just try putting the route in. I'm slightly uncomfortable using restful routes when serving html. In the project i'm working on i've been trying to preserve those for data/json transmission, in order to support outside api action.
I think your routes setup recognizes create as {user} in user/{user}, thus redirect to user/show/create as your "custom route for showing a user" setup.
You may have to avoid getting string variable right after users/ route.

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.

Laravel4 route pattern error

I´m using laravel 4 for a cms project, and i´m having some problems with my routes...
These are my current routes
Route::get('/', 'IndexController#showNews');
Route::get('/logout', 'UserController#logout');
Route::resource('/login', 'UserController');
Route::resource('/user', 'UserController#index');
Route::resource('/user/{route}', 'UserController');
// Routes that shows us the pages...
Route::get('/{page}', 'IndexController#showPage');
Route::get('/{page}/{id}', 'IndexController#showPage');
To my user routes i have a custom router that routes the user information around, not really a problem. But all of that works great, but when i try to navigate to "/test" Wich would link to a test page, it gives me this error.
Route pattern "/user/{route}/{{route}}" cannot reference variable name "route" more than once.
It comes up to router logic, and i´m fairly new to laravel. Is there a way for me to work around this problem? It´s a collision between the user/route and the /route wildcards.
Route::resource('/user', 'UserController#index');
Route::resource('/user/{route}', 'UserController');
The problem is that you are using Route::resource to declare the routes, while by using Route::resource you are actually declaring a RESTful controller with a table of actions to be handled by Laravel automatically. You are using it incorrectly.
See the docs to see which routes are handled in the background (and hence the source of the conflict):
Take a look at the table called Actions Handled By Resource Controller
For any route handler that is not within the table you will have to declare separate routes. Something like:
Route::get('foo/filter/{filterName}/{filterValue}',
array('as'=>'filteredroute','uses'=>'FooController#filter'))
As a summary, Route::resource enables you quick CRUD RESTful access.

Change Laravel's default (root) controller

In Laravel the default controller is the Home_Controller. However I have a controller called frontend. I want to use this instead of the home controller.
When I register a route like this:
Route::controller(Controller::detect());
then a request to /offer will be handled from within the home controller like home#offer. I want to use frontend#offer and access it from the site's root - not like /frontend/offer.
What should I do?
Thanks in advance.
Home_Controller is one of the hard-coded convention which exist in Laravel 3, however there are still ways to define routing to point the Frontend_Controller methods, my preference would be.
Route::any('/(index|offer|something)', function ($action)
{
return Controller::call("frontend#{$action}");
});
Limitation with this is that you need to define all supported "actions" method in Frontend_Controller.
My guess is that the only reason you think the Home_Controller is some sort of default is because you are using Controller::detect(); I really haven't seen anything in the documentation to make me think that the Home_Controller is anything special at all. In fact, it doesn't even look like it is routed to in the example documentation. Given that, my first suggestion would be to get rid of Controller::detect() and see if that fixes your problem.
Barring that, have you tried registering frontend as route named home? It appears that all URL::home() does is search for the 'Home' route, and then redirect to it. When using controller routing this can be done with something to the effect of.
Route::get('/',
array(
'as' => 'home',
'uses' => 'frontend#index'
)
);
Or is that not your desired effect? Do you want all routes which aren't otherwise found to be redirected to your frontend controller?
If you are concerned about your urls looking pretty, you can probably use some rewrite rules in your .htaccess file to make the whole process of routing to /frontend/index transparent you your users.
Add this to your routes.php :
Route::get('/', array('as' => 'any.route.name', 'uses' => 'frontend#offer'));
If you have any other / route, just remove it.

Categories