dtsc1684 2011-05-02 15:44
浏览 38
已采纳

具有多语言支持的PHP路由

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 access 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?

  • 写回答

2条回答 默认 最新

  • dongtuan1594 2011-05-02 16:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?