Are PHP MVC Controllers implemented by url rewrite

2019-04-17 02:51发布

The controller in a Java EE application may be represented by a servlet, which may be currently implemented using JavaServer Faces (JSF).

But in PHP there is no such servlet,so I guess is it implemented by url rewrite?

So that every request is directed to that controller?

5条回答
一纸荒年 Trace。
2楼-- · 2019-04-17 03:09

It can be done with mod_rewrite but mostly in php there is a front-controller mechanisim which does all controlling through a single file. In this way, all controllers are specified in the url. See this for more explanation about controllers and mvc in php.

查看更多
相关推荐>>
3楼-- · 2019-04-17 03:25

As most of the other answers have shown, usually mod_rewrite is the way to do it. But, if you don't want to use mod_rewrite, you can let your bootstrap file actually configure variables from the URL itself.

I use a couple of my own functions which creates an array from a URL, so:

site.com/page/welcome/param1/param2

becomes

$url[0] = 'page'
$url[1] = 'welcome'
$url[2] = 'param1'
$url[3] = 'param2'

and then I can pass the $url array to my Router and it decides which parts of the Controller/Action/Param call each element belongs to.

The same URL request shown above creates the Controller/Action/Param call:

// http request for site.com/page/welcome/param1/param2
$controller = new Page_Controller('param1', 'param2');
$controller->welcomeAction();

while, depending on the settings in my Router object, I can create subdirectories, such as for calls to 'admin/':

// http request for site.com/admin/page/welcome/param1/param2
$controller = new Admin_Page_Controller('param1', 'param2');
$controller->welcomeAction();

With url_rewrite I think it would be harder (still possible though) to add those reroutes in, and with my method (Some frameworks might also do it, not too sure) it allows you to customize it more, ie you can edit the $url array if needed before passing it to the Router object.

I'm not sure what the negatives are in using this method, but it works pretty well for me!

查看更多
做个烂人
4楼-- · 2019-04-17 03:26

MVC in PHP typically makes use of a front controller, which serves as the only entry point into the application.

This is usually implemented by using mod_rewrite to point all requests to a php file containing your bootstrap code.

This bootstrap code will contain your front controller.

The front controller takes it from there, redirecting the request based on the input parameters to the appropriate controller. Your target controller is usually specified as one of the parameters.

So, when you hit:

http://yourdomain.com/blog/new

The application would redirect the request to your bootstrap code with your front controller, which will interpret this input to mean 'blog' is the requested controller, 'new' is the action. It would then instantiate the 'blog' controller, pass it the 'new' action, and the rest is standard MVC.

查看更多
forever°为你锁心
5楼-- · 2019-04-17 03:29

I think that's called the Front Controller pattern http://en.wikipedia.org/wiki/Front_controller and usually is achieved via mod_rewrite rules that any requests for phisically nonexisting files is redirected to index.php which then decides what to do.

查看更多
乱世女痞
6楼-- · 2019-04-17 03:33

You would use mod_rewrite to redirect everything to the index.php file. So you use mod_rewrite if you want this:

http://example.com/page/welcome

and not

http://example.com/index.php?page/welcome

or

http://example.com/index.php?controller=page&action=welcome

查看更多
登录 后发表回答