Multi-language URLs in Laravel 4 - laravel

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 :)

Related

Calling a controller/action through query parameters

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

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

How to run single controller method from any route in Laravel 4

I really hope my question has been well thought out but here goes.
How do you implement something like
Route::get("/url1", "controller#method");
Route::get("hello/url1", "controller#method");
Route::get("hello/hi/url1", "controller#method");
in Laravel but using something like
Route::get("*/url1", "controller#method");
instead of declaring every route path?
I will explain why this problem has come up. You see the primary url is always changing because its being called from a js file via a location.href call. I could decide to use a primary url variable but its to be deployed via intranet to different servers in organizations and the primary url could change at any time meaning that localhost/project on one system might become localhost:7987/project on another thus breaking the url variable, now thats on one part. On the other hand there are js functions running continuously and when someone navigates to a deeper url, say from localhost/home to localhost/home/event a route call that should be independent of folder breaks
So yeah, I am wondering if theres a way to declare a global route that points to a controller and/or if this is possible in Laravel.
Thanks
Try this:
Route::get('{something}/url1', 'controller#method')->where('something', '*');
Not sure if that will work, but the idea is that you can use where to pass some Regexp to match selected value from route.

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,

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