URL mapping in PHP?

2019-01-13 13:56发布

I come from a Java background and with any servlets-based technology, it's trivial to map a range of URLs (eg /reports/, /secure/.do) to a specified servlet. Now I'm less familiar with PHP but I haven't yet seen anything that does quite the same thing with PHP (or mod_php). It's entirely possible that I'm missing something simple.

How do you do this in PHP?

One of the reasons I want to do this is "one use" URLs. Now this can sorta be done with GET parameters (like an MD5 hash token) but I'm interested in URL mapping as a general solution to many problems.

Another big reason to use something like this is to have RESTful URLs.

标签: php apache rest
7条回答
放我归山
2楼-- · 2019-01-13 14:10

Can't be done in PHP as such. Note that in Java, it's not the Servlets either that map URLs, but it's the configuration of the servlet container (e.g. Tomcat).

You can achieve similar results for PHP applications by using URL rewriting.

查看更多
Summer. ? 凉城
3楼-- · 2019-01-13 14:15

Options for HTTP servers other than Apache:

If you are using Lighttpd, then their mod_rewrite module has a completely different configuration syntax. See http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite for details.

If you are using IIS, then you need ISAPI_Rewrite (http://www.isapirewrite.com/) or the URL Rewrite Module (http://learn.iis.net/page.aspx/460/using-url-rewrite-module/).

查看更多
迷人小祖宗
4楼-- · 2019-01-13 14:15

This sounds like something that mod_rewrite would typically be used for (as a part of Apache, not PHP.) I have not heard of anything in PHP which would accomplish this.

查看更多
三岁会撩人
5楼-- · 2019-01-13 14:19

As others have mentioned you can use mod_rewrite to do this but also you could use a front controller design pattern which is what many of the web frameworks use. Take a look at Horde routes or Zend_Controller_Router_Rewrite which can be used for MVC,

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-13 14:28

With Apache, you are able to setup URL Rewriting for your php pages with mod_rewrite, check this resources:

查看更多
时光不老,我们不散
7楼-- · 2019-01-13 14:29

Other than using mod_rewrite, as already reported you can do a little of magic with a simple trick.

Put into the .htaccess a directive like this one

<FilesMatch "^servlet$"> 
  ForceType application/x-httpd-php
</FilesMatch> 

substitute ^servlet$ with a regular expression of your choice (it will be the name of your dispatcher)

The file servlet should be similar to this

<?php
  $data = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); // $data[0] always empty
  $fileToInclude = $data[1].'.php';
  if (file_exists($data[1]) {
     $params=array_slice($data,2); // you can do here something more sophisticated
                                   // for example sanitize parameters or assemble 
                                   // an hash
     include ($fileToInclude);     //Think to this file as a servlet
  } else {
    // issue a 404 error, maybe one of the 500 series
  }
?>

The url can have the form: http://yoursite/servlet/reports/sales/2009 you can also reach the form http://yoursite/reports/sales/2009 plaiyng a little with the .htacces and the dispatcher.

This method has the advantage that mod_rewrite is not required as FilesMatch (1.3+) and ForceType (2.0+) are in the apache core

See for reference
http://httpd.apache.org/docs/2.2/mod/core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/core.html#filesmatch
http://www.devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

查看更多
登录 后发表回答