Calling a controller/action through query parameters - laravel

Is it possible to call a controller and action directly by using query parameters in Laravel? I see some frameworks allow /index.php?_controller=X&_action=Y, or Yii allows /index.php?r=X/Y. I was wondering if something similar was possible in Laravel/Symfony.

symfony
The query string of a URL is not considered when matching routes. In this example, URLs like /blog?foo=bar and /blog?foo=bar&bar=foo will also match the blog_list route.
https://symfony.com/doc/6.0/routing.html
laravel afaik doesnt support that either
you are obviously free to just forward yourself
e.g. write one router that forwards to other controllers
symfony https://symfony.com/doc/6.1/controller/forwarding.html

Related

Filters in Laravel 4

I'm trying to use a pattern based filter in my routes file.
I want to stop anything without crsf and only allow ajax calls.
So far I have:
Route::when('/gateway/*', 'crsf');
I'm not sure where im going wrong and how i could add in the ajax verification.
You can use Route Groups to apply before filter.
http://laravel.com/docs/routing#route-groups
You can check if a request is ajax or not by calling if(Request::ajax). Define this on your before call.
http://laravel.com/docs/requests
As for the patterns, have a look at Pattern Based Filters section of Routing documentation.
http://laravel.com/docs/routing

Multi-language URLs in Laravel 4

I am trying to implement multi-language URLs. Thus I want to have URLs like:
/de/ueber-uns/kontakt and /en/about-us/contact
So far so good, I use App::before() in filters.php to check the locale given. I think I then need a route in routes.php for every controller action in every language.
So I thought of dynamically creating the file routes.php. All I would need for it is to know how I can access all available controllers or get all registered routes in code (like artisan routes but not with CLI).
So the questions are:
is the general approach for multilingual urls correct?
is it possible to access all controllers to extract the methods somehow?
how could I get the RouteCollection that is used within \Illuminate\Routing\Router.php?
Thank you in advance!
I ended up doing the following:
1) Routes in routes.php are dynamically created with a custom artisan command. It parses all Controllers and creates routes for every action in every language that is supported. The language string is handled with routes like
Route::get('{lang}/customer/login', 'CustomerController#getLogin')->where('lang', '[a-z]{2}').
This way users can just change the language string and the site will load in the correct language (if supported).
Routes for different languages all lead to the same controller action. For these languages except english, I need translations (routes.php in /app/lang).
2) a before filter for those controllers whose actions get translated is set in constructor. It basically checks if the language string is valid and replaces it if not. The chosen language will be set in the session.
I hope anybody can use it :)

Asp.Net Web Api - Change parameter name

In my team we have coding rule that requires that every function's parameter starts with prefix, e.g. *p_someParam*.
With Web Api if we want to request a GET function that takes two parameters, we should add those parameters like "...?p_firstParam=value1&p_secondParam=value2".
Is there some way to use in requests more user-friendly names, like someParam without prefix, that will automatically map to parameters in controller's action? Maybe there is some attribute to rename action parameters? I couldn't find any similar example.
Every clue is appreciated.
I think you looking for URL rewriting, in that you need to map the urls to config or programmatic
http://www.codeproject.com/Articles/2538/URL-Rewriting-with-ASP-NET nice article to follow, its in ASP.Net,

MVC subController

My application has the following module:
/groups/{id_group} -- > GroupController.php
My question is this,
/groups/{id_group}/events/{id_event}
Which controller should handle this?
If you are using groups/n/ as routing, you would want to use your framework's routing system to route to the event controller. Otherwise, it is the groups controller.
If you want to stick to just using the first parameter as the Controller, then consider using the further parameters as purely parts of the query string e.g
?controller=groups&itemId={id_group}&action=events&actionId={id_event)
and use the controller to route it to the correct place e.g.
$model->getGroup($id_group, 'events', $id_event);

Should I still use querystrings for page number, etc, when using ASP.NET 4 URL Routing?

I switched from Intelligencia's UrlRewriter to the new web forms routing in ASP.NET 4.0. I have it working great for basic pages, however, in my e-commerce site, when browsing category pages, I previously used querystrings that were built into my pager control to control paging and now am not sure how to handle this using routing.
I defined a MapPageRoute as:
routes.MapPageRoute("cat-browse", "Category/{name}_{id}", ~/CategoryPage.aspx");
This works great. Now, somebody clicks to go to page 2. Previously I would have just tacked on ?page=2 to the url. How do I handle this using web forms routing? I know I can do something like:
http://www.mysite.com/Category/Arts-and-Crafts_17/page/2
But in addition to page, I can have filters, age ranges, gender, etc.
Should I just keep defining routes
that handle these variables like
above?
Should I continue using querystrings
and if so, how do you define a route
to handle that?
The main reason to use url routing is to expose clean, user-and-SEO-friendly, URLs. If this is your goal, then try to stick to it and not use querystring parameters. Note: I don't believe we need to completely ban the use of querystrings and, depending on your situation, you may decide it best to use querystring parameters for parameters that are not used frequently, or where no real value is added by making the information more semantically meaningful.
So here's what I would do:
Define a catch-all for all your other parameters:
routes.MapPageRoute("cat-browse", "Category/{name}_{id}/{*queryvalues}", "~/CategoryPage.aspx");
In /CategoryPage.aspx, access the router parameter and then parse as appropriate:
Page.RouteData.Values["queryvalues"]
Instead of using the syntax of Arts-and-Crafts_17/**page/2/age/34** for these parameters, I perfer to use the following syntax: Arts-and-Crafts_17/pg-2/age-34/
If you do this, the catch-all parameter 'querystring', will equal pg-2/age-34. You can now easily parse this data and add each name/value to the page context. Note that you will need to do something along these lines since each of these parameters are optional on your site.
You can take advantage of C# 4.0 named and optional parameters. Please have a look at this example from haacked
If you are using a lower version of the framework, you can also use code from the link above. But instead of declaring the method as
public ActionResult Search(int? page=0)
{}
you can declare it as
public ActionResult Search(int? page)
{
if(page == null)
{
page=0;
}
}
HTH

Resources