Multi-language PHP routing

I am currently creating a CMS as part of. I made a .htaccess file that looks like this:

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([A-Za-z]+)/(.*)$ index.php?url=$1 [PT,L] </IfModule> 

My problem is that:

1) Access to my pages is possible using the usual MVC URL: http://example.com/pages/view/ {the-shorttag-of-the-page}

2) My pages are also available along the route: http://example.com/p/ {the-shorttag-of-the-page}

What I really need to implement is that I can access it in two ways: http://example.com/ {the-shorttag-of-the-page} Or with support for the language http://example.com / {lang} / {the-shorttag-of-the-page}

My problem is that I do not want to change mod_rewrite - I rather want to create routes in PHP, but I can not find a solution on how to direct the PageController-> view () method without having a static letter ("p" in the upper example) in conditions.

Do you understand my question? And do you have a few tips that could lead me in the right direction?

+4
source share
2 answers

It looks like you need to implement some kind of router.

I came across this myself, and that’s how I did a good job:

Some classes of routers parse URLs. It explodes parts of the URL ( /en/controller/action/param1/paramval1/ ) into pieces. It defines the language code (default, if not specified), controller, action, etc. The router also provides information (controller, action names, parameters) for other parts of the system, although for this purpose you can implement a special class (for example, Request).

Now different parts of the system have access to this information through the Router or Request class. From here it is easy to implement a multilingual site.

Once again, the important part is displaying the URL for the controller and action. Specifically, if you don't need a URL such as /controller/action/parameter1 , you should have a mapping table (XML table / database table) that maps the stub (short page tag) to the controller and action. This is a router that reads this table and determines the parameters of the controller, actions and other parameters based on these rules.

Hope I didn't complicate too much :)

Update:

For me, I have implemented mapping to an XML file. Let me explain further: the router takes care of URL resolution. Typically, your URL will contain the name of the controller and actions (in your case, the pages are the controller, the view is the action).

Now in your case you just want a stub, so the router must somehow get information about which controller and action is being called. It is the purpose of the mapping to map the stub to the appropriate controller and action.

I did this as static routes, written manually in an XML file, but you can, for example, use some kind of plugin class that checks if stub really refers to the page. If this is not the case, this can mean two things: is the controller or the request is invalid.

Let me talk about collisions: if stub matches the name of any controller, you have a collision. Should the page with the given dummy be displayed or should the controller be called? This should somehow solve, perhaps you can programmatically limit the user to a page with such stubs.

In addition, the next time you immerse yourself in application development using PHP, I would recommend that you use some well-known PHP framework (if you have not already done so). Zend, Kohana or CodeIgniter. All of them provide a router and many other components that greatly simplify, speed up and improve application development.

+1
source

I do this by passing the entire line after the domain to the index page and processing it in php and not in .htaccess. I am not an .htaccess expert, but this works for me:

 RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^.*$ ./index.php 

I indicate in the configuration if the site will or will not use lang support, I think that support on the same site can be difficult to implement.

Please ask me if you need more information.

UPDATED

Dennis, I read the @usoban post, and I could say that I have a similar approach. As I said, I get the full path after the domain from .htaccess to my application class and parse it into an array. If "multilanguage" is specified in config, I take the first element of the array and load the "alias" table for this language.

The table 'alias' is an associative array that displays the alias-controller, for example:

 $lang_mods = array( 'home' => 'Home', 'contact' => 'Contact', 'sitemap' => 'Sitemap', 'about' => 'Section/about', 'terms' => 'Section/terms', ); 

This way I can have a fully translated URL, i.e.

http://www.example.com/en/contact and also: http://www.example.com/es/contacto

So, after removing the first element of the language from the url array, I check the next element on the alias table. The last two elements in the table are special cases that you can use to simplify the URL by getting

http://www.example.com/en/about instead of http://www.example.com/en/section/about this looks better, but both point to the same Section controller.

After deleting the language and alias, the remaining elements of the -if any- array are passed to the controller. The 'about' or 'terms' in this example are added to the array of arguments. Depending on the controller, the first argument may or may not be an action.

If at some stage of this process something does not match the valid values, it is redirected to the main page.

Well, in your case, maybe if the first argument (or the second, if the site is multilingual) can be checked against the available controllers, if it does not exist, you can redirect to the "standard" controller and then check for available pages in your db . That way, you could avoid having to have an extra /p/ in the url.

I tried to be clear, sorry, English is not my native language;)

+1
source

All Articles