开发者

PHP Routing with multilingual support

开发者 https://www.devze.com 2023-03-02 20:12 出处:网络
I am currently creating a CMS on a framework. I have made my .htaccess file, which looks like this: <IfModule mod_rewrite.c>

I am currently creating a CMS on a framework. I have made my .htaccess file, which 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) My pages can be accessed with the normal MVC URL: http://example.com/pages/view/{the-shorttag-of-the-page}

2) My pages is also accessible with a route: http://example.com/p/{the-shorttag-of-the-page}

What I really need to implement is that I can acces开发者_JAVA技巧s it in two ways: http://example.com/{the-shorttag-of-the-page} Or with language support http://example.com/{lang}/{the-shorttag-of-the-page}

My problem is that I don't want to change the mod_rewrite - I rather want to make the routes in PHP, but I can't find a solution of how to route to the PageController->view() method without having a static letter ("p" in the upper example) in the conditions.

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


This sounds like you need to implement some kind of router.

I've faced it myself and this is how I made it work quite good:

Some router class parses the URL. It explodes URL parts (/en/controller/action/param1/paramval1/) into pieces. It determines language code (default if not provided), controller, action, etc. Router also provides information (controller, action names, parameters) to other parts of the system, although you may implement special class for that purpose (eg. Request).

Now different parts of the system have access to given information via Router or Request class. From here on, it should be easy to implement multilangual site.

Once again, the important part is mapping URL to controller and action. Definitely, if you don't want URL like /controller/action/parameter1, you should have some mapping table (XML document / database table) that maps a stub (page short tag) to controller and action. It is the router that reads this table and determines controller, action and other parameters based on the given rules.

Hope I didn't complicate too much :)

Update:

As for myself, I've implemented mapping with XML file. Let me explain further: router takes care of resolving the URL. Normally, your URL would contain the controller and action name (in your case, pages is the controller, view is the action).

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

I've implemeted this as static routes, written manually into XML file, but you could for example use some kind of plugin class that checks if stub actually relates to a page. If it doesn't, that can mean two things: it is a controller or request is invalid.

Let me talk further about collisions: if stub is the same as the name of any controller, you have a collision. Should you render a page with a given stub, or call the controller? That has to resolved somehow, perhaps you can programatically restrict user to have a page with that kind of stubs.

Also, next time you dive into application development with PHP, I'd recommend you using some well-known PHP framework (if you don't already, ofc.), e.g. Zend, Kohana or CodeIgniter. They all provide router and many other components that make developing applications much easier, faster and more secure.


I'm doing that by passing the entire string after the domain to the index page, and processing it in php, not in .htaccess. I'm not an .htaccess expert, but this is working for me:

RewriteEngine On  
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^.*$ ./index.php

I specify in the setup if the site will or will not use lang support, I think support both in the same site could be hard to implement.

Ask me please if you need more details.

UPDATED

Dennis, I was reading @usoban post and I could say 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 to an array. If 'multilanguage' is specified in config, I take the first array element and load the 'alias' table for that language.

The 'alias' table is an associative array that map alias-controller, like this:

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

In this way, I can have a fully translated url, i.e:

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

So, after removing the first language element from the url array, I check the next element against the alias table. Last two items in the table are special cases that can be used in order to simplify the url, getting

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

After removing language and alias, the remaining elements in the array -if any- are passed to the controller. 'about' or 'terms' in the example, are prepended in the argument array. Depending on the controller, the first argument can be an action or not.

If in some step of this process, something doesn't match the allowed values, it's redirected to the home page.

Ok, in your case, maybe if the first argument (or the second if the site is multilanguage) could be checked against the available controllers, if it doesn't exist, you could route to a 'default' controller, and then checking against the available pages in your db. In this way you could avoid the need of having an additional /p/ in the url.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号