How to do MVC form url formatting?

2019-04-11 11:11发布

I am using PHP. I want to create an MVC setup from scratch to learn more about how MVC works. I want to use clean urls with slashes as delimiters for the arguments. How do people do this when it comes to GET method forms? Or do people avoid GET method forms all together?

As of right now the ways I can imagine are:

  1. Don't use GET method forms (although this makes it harder to let users bookmark/link in some cases).
  2. Use AJAX instead of form submission (although what do you do for SEO and JS disablers?).
  3. Have page submit to itself with post method, then reform the post vars into an url, then rerout to that url using headers (seems like wasted resources).

Any suggestions or suggested reading welcome.

10条回答
趁早两清
2楼-- · 2019-04-11 11:21

You can use a .htaccess file like this

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

So... if the url is

http://example.com/controller/action/param1/

you can path the controller and the action, the index.php receive the var url [string]
and you can split them to load the controller...like

$params = explode('/', $_GET['url']);
$controller = new $params[0];//load the controller
$controller->$params[1]($params[2]);//ejecute the method, and pass the param
查看更多
Summer. ? 凉城
3楼-- · 2019-04-11 11:24

If you are interested in understanding how MVC frameworks / principles work sometimes the best way is first to understand how to implement them (something like CodeIgniter) you can then delve into the source code / documentation and understand how it works.

If you want to jump in at the deep end look into reading up on MOD_REWRITE to solve your URL concerns. GET is usable - you will just rewrite the URL components.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-11 11:26

Cfreak the best way to answer this question would be for you digg into the various PHP frameworks source code to see how they've implemented an MVC approach. The PHP frameworks I've used typically exclude GET data and rely soly on POST data. However information can be passed via a query string see example below.

Typically URL's in PHP MVC frameworks look something like this: example.com/index.php/controller/action/arguments. The index.php part is usually removed by using an .htaccess file. You controller is a class, that most likely inherits from some sort of parent controller class. You controller has actions/methods whatever you prefer that are called next. You can pass these methods arguments, typically an unlimitied number of arguments by tacking them onto your URL. I hope that helps to give you a basic idea of the structure. Everything starts in index.php, which will include all your necessary config files, and load and required classes you may need :)

查看更多
欢心
5楼-- · 2019-04-11 11:36

The question you are basically asking is how to achieve SEO-friendly(Search Engine Optimization Friendly) URLs, in the format "www.site.com/controller/action/params". This is achieved through editing your .htaccess file in a few key folders to redirect server to specified folders to gain a common access point for your web application, using MOD_REWRITE. I would take a look at this tutorial. The first few paragraphs explain how to achieve the URLs you are looking for.

查看更多
登录 后发表回答