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

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别