简体   繁体   English

编写一个基本的PHP MVC,不知道如何开始

[英]Writing a basic PHP MVC, not sure how to start

I am working on a personal project based in PHP and MySQL, and I am doing a bit of research and playing around with rewrites. 我正在开发一个基于PHP和MySQL的个人项目,我正在做一些研究并玩弄重写。 Say I have a site... 说我有一个网站......

http://www.myDomain.com/

And I want to have an index.php, or bootstrap, in the root of the domain. 我希望在域的根目录中有一个index.php或bootstrap。 So if you access... 所以,如果你访问...

http://www.myDomain.com/admin/

It will still load from the index.php in the top level of the domain, which handles the parsing and loading of configuration files, and redirecting the user to the correct location, making pretty links along the way. 它仍然会从域顶层的index.php加载,它处理配置文件的解析和加载,并将用户重定向到正确的位置,沿途制作漂亮的链接。

Where should I start in my research and education on this? 我应该从哪里开始我的研究和教育? I'm at a loss somewhat. 我有些不知所措。 Thank you for your time :) 感谢您的时间 :)


Update: 更新:

Sounds like I do want to move towards a MVC system with a front controller. 听起来像想向MVC系统与移动前端控制器。 Any good references on writing my own MVC framework (would be very basic). 关于编写我自己的MVC框架的任何好的参考(将是非常基本的)。 I honestly don't want to pull in the Zend Framework at this time (would bulk it up a lot!) 老实说,我不想在此时使用Zend Framework(会大量增加它!)

Basically, you rewrite any incoming request to your index.php . 基本上,您将任何传入的请求重写到index.php Here's an example .htaccess from the Kohana framework: 这是Kohana框架中的一个示例.htaccess

# Turn on URL rewriting
RewriteEngine On

# Protect application and system files from being viewed
# RewriteRule ^(application|modules|system) - [F,L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]

So your example of would be routed to index.php/admin . 所以你的例子将被路由到index.php/admin Then you can look at $_SERVER['REQUEST_URI'] to determine what to do next. 然后你可以查看$_SERVER['REQUEST_URI']以确定下一步该做什么。

A relatively common pattern would be to use the first segment of the URI as the controller, and the second as the method. 一个相对常见的模式是使用URI第一段作为控制器,第二段作为方法。 So for example: 例如:

$segments = explode($_SERVER['request_uri'], '/');//array('admin')

if(isset($segments[0]))
{
    $class = $segments[0].'_controller';//'admin_controller

    if(isset($segments[1]))
         $method = $segments[1];
    else
         $method = 'index';
}
else
{
    $class = 'index_controller';
    $method = 'index';
}

$controller = new $class;
$controller->$method();

That code is by no means production ready, as it would die a fiery death, if for example the user visited a URL for a non-existent controller. 如果例如用户访问了不存在的控制器的URL,那么该代码绝不是生产准备好的,因为它会死于火热的死亡。 It also doesn't do nice things like handle arguments. 它也不会像句柄参数那样做得很好。 But it's kind of the idea behind how a PHP MVC framework operates. 但这是PHP MVC框架如何运作背后的想法。

By the way, another name for what you're calling bootstrap is front controller . 顺便说一下,你所谓的bootstrap的另一个名字是前端控制器 You can google that term to find a lot more information about the pattern. 您可以谷歌该术语以查找有关该模式的更多信息。

You will need to look at configuring your .htaccess to internally rewrite all requests to your bootstrap file, which may be index.php 您需要查看配置.htaccess以在内部重写对引导程序文件的所有请求,这些请求可能是index.php

Kohana uses this to do it Kohana用它来做到这一点

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

You can then access $_SERVER['REQUEST_URI'] to begin routing requests to controllers. 然后,您可以访问$_SERVER['REQUEST_URI']以开始将请求路由到控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM