Phalcon routing behaviour - redundant routes? - php

I'm working with the following routing example:
https://github.com/phalcon/mvc/blob/master/simple-subcontrollers/app/config/routes.php
Can anyone explain to me why on line 12 there is this:
$router->add('/:controller', array(...
Haven't we already defined the route on line 5 as:
$router->add('/:controller/:action/:params', array(
"/:controller" is a subset of '/:controller/:action/:params'.
If you remove "/:controller" route, URLs ending with controller name will not be matched.
Why isn't router falling back to default action name that I can indicate as:
$router->setDefaultAction('index');
Is this a bug or a feature?
Thanks!

This route
$router->add('/:controller/:action/:params', array(
Will only execute if it gets all parts to make the route valid for example
myapp/users/logout/dave
The above will match what that route is looking for and cause the route to execute.
myapp/users
The above however wouldn't match due to missing actions and params also a url with a / and without aren't the same url.
Since you can add many routes as you need using add(), the order in which routes are added indicate their relevance, lastest routes added have more relevance than first added. Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.
Hope this helps.

Related

Laravel how to generate urls using GET route names?

I got this 2 routes in my routes file (web)
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
Route::post('management/special-fees/add', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
They both share the same name but one is GET and the other is POST, so far so good. But now I want to make an url in my view to open the form, for that I use the method route() like this
route('management/special-fees/add',$user->id )
but when trying to go to the url I get this route
.../management/special-fees/add?5
there is a question mark instead of a "/" so the route is invalid.
I made some tests and I figured out that happens because is trying to go to the POST route instead of the GET one if I change the POST route's url in the web file like this
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
I will in fact get this url
.../management/special-fees/addSSSS?5
So why is the route() method generating a url for the POST route over the GET one? how do I make it to choose the GET route first?
In laravel the routing is prioritized by in the order it is written in your route.php file.
In this case you're writing the Route::post last, which in turn tells Laravel that that one should have the highest priority. Try switching them and the Route::get will have the higher priority.
Like so:
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
I may be wrong, but I think you'll have to re-think route naming. One of the problems route naming helps eliminate is redundant and complex names. For example, if you looked at route:list for Route::resource('something', 'SomethingController') it will have something.index, something.store as route names for Route::get('something') and Route::post('something').
If it's the same name, it will always resolve to the first one and will probably never hit the second route; in your case will hit the POST route and never the GET route.
?5 means 5 is an argument for your get route.
try this
url('management/special-fees/add/'.$user->id)
for get route insted of
route('management/special-fees/add',$user->id )

Laravel route not matching pattern

In my Laravel routes/web.php file I have defined the following two routes:
Route::get('transaction/{id}', ['uses' => 'PaynlTransactionController#show'])->name('transaction.show');
Route::get('transaction/{txId}', ['uses' => 'PaynlTransactionController#showByTxId'])->name('transaction.showByTxId');
In my RouteServicesProvider I have defined the following two patterns:
Route::pattern('id', '[0-9]+');
Route::pattern('txId', '/^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$/');
Whenever I go to transaction/<id> the routing works correctly, as long as id is an integer. However, when I go to transaction/TX874-152268, for example, it doesn't match any route and I receive the NotFoundHttpException in RouteCollection.php error.
I've validated the txId regex and it gives a full match: https://regex101.com/r/kDZR4L/1
My question: how come only my id pattern is working correctly, whereas my txId pattern isn't?
In the route
Route::pattern('txId', '/^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$/');
I had included the forward slashes at the start and end of the string. This should not be included when passing a pattern to Route::pattern. Thus the following works:
Route::pattern('txId', '^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$');
Because the urls are both /transaction/{value} it will get the last on.
If you change /transaction/{txId} to /transaction/tx/{txId} it will be clear for the routes.
Routes can only get one, so when you assign the prefix (at this time /transaction) to both of the urls it doesn't work.
You can also use /transaction/TX{txId}, in your controller you can past TX before the txId variable.
public function showByTxId($txId) {
$txid = "TX".$txid;
}
Edit:
Remove the / add the start.
Route::pattern('txId', '^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$');
Hope this works!

Laravel 4 only root route works - other routes return 'Controller method not found'

I've got laravel set up on a domain on a linux host and I have a WAMP local host set up.
The only route that works is the root, when ever I try go to another route such as domain.com/account I get a "Controller method not found." error.
In my routes.php file I have:
Route::controller('','LoginController');
Route::controller('account', 'AccountController');
In my LoginController, I have just two methods. getIndex and postIndex.
After a couple of hours Googling with no results and playing around with the routes file amongst things, still nothing worked.
I tried adding the below route which didn't work either.
Route::any('hello', function(){
return 'hello!';
});
However, I then commented out my Route::controller('','LoginController'); line and the other routes started working!
I then changed it to Route::controller('login','LoginController'); and this and the other routes still worked. I then changed it to Route::any('','LoginController#getIndex'); and the root and other routes still worked. However, doing it this way, when I cliked the login button on my page nothing happened.
So my question really is, is there something wrong with doing Route::controller('','LoginController');? As everything else seems to 'work'
Laravel save an internal collection of registered routes in the $routes member of the Router class. When dispatching a request, a process of picking each element from this collection and test with current request will be executed to find out which route will be handle. This process is affected by the order of your route registering statements.
When testing each route with the current request, the picked route will be compiled and have a regex pattern. This pattern will be use to check with the current URI by the preg_match function as you can see at this line in Laravel source.
When using Route::controller a special route will be add to your routes collection. If your input is Route::controller($uri, $controller) then this special routes will have a regex pattern as ^/$uri/?P<_missing>(.*)$ and it tells Laravel that this request belong to a missing method of the $controller controller class.
In your case, you have set the value of $uri to an empty string which cause the regex of the special route to be ^/?P<_missing>(.*)$ (setting $uri with the string / cause the same effect). Well, this regex will match every URI. So, the internal route looking up process will abort when look to this special route. This is the reason while the exception has been thrown.
You should not use an empty string or the/ string when register with the Route::controller method like the way you did. Instead, use Route::resource or explicit calls (Route::get, Route::post, ...) to handle your top level routes.
I have not tried that, but maybe you could add a "/":
Route::controller('/','LoginController');
Edit
I was able to reproduce the issue and I solved by changing the order of your route lines:
Route::controller('accounts', 'AccountController');
Route::controller('','LoginController');

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.

RESTful trailing slash routes in laravel

I have the following routes in my Laravel 3 RESTful api project
Route::delete('/(:any)', 'resources#destroy');
Route::delete('users/(:any)', 'users#destroy');
The problem I am having is when a user sends a delete request to /users/
What I want to happen is that the users#destroy route is called with parameter null. In my controller I have an exception for a user trying to delete a null resource.
What seems to be happening is that the resource#destroy route is called with parameter users. This obviously has the undesired affect of deleting the users resource.
I know I could modify my .htaccess but technically /users/ does belong to the users controller not the resources controller. I want to maintain that relationship.
I was wondering if there is a simple way to solve this from within Laravel?
EDIT: have the above working with the answer below. Now I have an error in my get routes
Route::get('users/(:any?)', 'users#show');
Route::get('users', 'users#index');
/users and /users/ both call users#index which I don't want.
I need GET /users to go to users#index and GET /users/ to go to users#show with null parameter
I worked around the trailing slash by adding a filter to my routes
Route::group(array('before' => 'trailingslash'), function()
{
//routes in here
});
Route::filter('trailingslash', function() {
$current = URI::full();
if(substr($current, -1) == '/'){
return return Response::error('404');
}
});
one point you need to consider.
Routes are defined without a front-slash. The only exception to this is the default route which is represented with only a front-slash.
so Route::delete('/(:any)', 'resources#destroy') will cause undesired result.
Moreover, your order is wrong.
(:any) will match user too and will send the request to resources controller.
so what you need to do is,
change the order (make it reverse).
change the route of resources considering according to the rules. like resources/delete etc.....
What I want to happen is that the users#destroy route is called with parameter null. In my controller I have an exception for a user trying to delete a null resource.
to do this, (after making the changes above....)
change the route of user/(:any) to user/(:any?) which will make the 2nd segment optional.
After that, straight forward.
$foo = URI::segment(2, null);
//Rest will follow.
EDIT
Now, the following code,
Route::get('users/(:any?)', 'users#show');
Route::get('users', 'users#index');
does not make any sense.
if i type user, what the router is suppose to take?
user#show with no optional segment or user#index?
Routes are design so that it removes the ambiguity. You are going in the opposite direction with making it all ambiguity.
just make a simple route.
like show
user/show
delete
user/delete
edit
user/edit
etc....
The type of routing you are applying is confusing for both users and developers as it carries ambiguity.

Categories