Home  >  Article  >  php教程  >  Minor【 PHP框架】1.简介

Minor【 PHP框架】1.简介

WBOY
WBOYOriginal
2016-06-16 08:39:25983browse

1.1 Minor是什么

  Minor是一个简单但是优秀的PHP框架,It just did what a framework should do. 只做一个框架应该做的,简单而又强大!

  框架Github地址:github.com/Orlion/Minor

  (如果觉得还不错给个star哦(^-^)V)

 

  作者: Orlion

  知乎:https://www.zhihu.com/people/orlion

  Github: https://github.com/Orlion

  

1.2 Minor有什么

  1.2.1 Minor主要提供了:

  1. MVC分层结构(实际上是没有Model层的,用户可以自己任意创建)
  2. Ioc(体现在Minor中就是类似于symfony/laravel的服务容器)
  3. Event(类似于symfony/laravel中的事件机制,是一个观察者模式的具体实现)
  4. Proxy(代理,是一个代理模式的具体实现,本人在创造Minor时是想实现类似Spring的AOP功能的,但是在写的时候发现如果要实现AOP则配置文件势必会变得冗杂,不利于开发所以就放弃了AOP,而提供了一个Proxy类)

  

  1.2.2 Minor为什么只提供了这么点东西?

  现在的PHP框架大多很臃肿,提供了各种功能,在本人看来这只是在重复造轮子(虽然写框架也是在重复造轮子...),框架只需要提供给用户一个架子和一些工具即可,不应该给用户一大堆东西(一大堆重复造的轮子),不应该捆绑用户而应该给用户极大自由,这样也会使用户更容易上手。个人觉得java的Spring框架就是一个很不错的框架,足够简单,足够强大,小即是美!而Minor就是在这种思想下诞生的框架。没有各种乱七八糟的分页、ORM、模板引擎、日志、队列等功能,就是一个很纯粹的“架子”。

 

  1.2.3 优点

  快! 性能高! 简单! 容易上手! 结构清晰!

  小即是美!

 

1.3 安装

  只需从github上(github.com/Orlion/Minor) git clone或者直接复制zip压缩包然后解压到项目中即可。

  Minor框架通过设置 <span class="token keyword">public<span class="token operator">/<span class="token punctuation">.htaccess</span></span></span> 文件去除链接中的index<span class="token punctuation">.php</span>。 如果你你的服务器使用的是 Apache,请确保开启 mod_rewrite 模块。

  如果是 Nginx 服务器,将下列指令放到网址的配置文件中:

location /<span style="color: #000000;"> {
    try_files $uri $uri</span>/ /index.php?<span style="color: #000000;">$query_string;
}</span>

  这样当你访问http://localhost/public/时就可以看到安装成功的画面了。 (当然你也可以配置一下把public去掉) 

 

1.4 Minor是如何运转的

  1.4.1 整体流程

  以访问网址http://www.servername.com/demo/test为例:

  1. 所有请求重定向到public/index.php文件
  2. 定义全局变量
    <span style="color: #008080;">define</span>('APP_DIR', <span style="color: #008080;">realpath</span>(__DIR__.'/../app/') .<span style="color: #000000;">DIRECTORY_SEPARATOR);
    
    </span><span style="color: #008080;">define</span>('PUBLIC_DIR', <span style="color: #008080;">realpath</span>(__DIR__) .<span style="color: #000000;">DIRECTORY_SEPARATOR);
    
    </span><span style="color: #008080;">define</span>('VENDOR_DIR', <span style="color: #008080;">realpath</span>(__DIR__.'/../vendor/') .<span style="color: #000000;">DIRECTORY_SEPARATOR);
    
    </span><span style="color: #008080;">define</span>('ROOT_DIR', <span style="color: #008080;">realpath</span>(__DIR__.'/../') .DIRECTORY_SEPARATOR);

     

  3. 根据配置文件来创建App对象:$app
    <span style="color: #800080;">$app</span> = \Minor\Framework\App::<span style="color: #000000;">getInstance(
        </span><span style="color: #800080;">$config</span>     = ['app' => <span style="color: #0000ff;">require</span> APP_DIR . 'Config/app.php', 'test' => <span style="color: #0000ff;">require</span> APP_DIR . 'Config/test.php'],
        <span style="color: #800080;">$providers</span>  = <span style="color: #0000ff;">require</span> APP_DIR . 'Config/providers.php',
        <span style="color: #800080;">$routes</span>     = <span style="color: #0000ff;">require</span> APP_DIR . 'Config/routes.php',
        <span style="color: #800080;">$events</span>     = <span style="color: #0000ff;">require</span> APP_DIR . 'Config/events.php'<span style="color: #000000;">
    );</span>

     

  4. App处理请求生成MinorResponse对象
    <span style="color: #800080;">$response</span> = <span style="color: #800080;">$app</span>-><span style="color: #000000;">handle(
        </span><span style="color: #800080;">$request</span> = \Minor\HttpKernel\MinorRequestBuilder::<span style="color: #000000;">buildMinorRequest()
    );</span>

     

  5. 向用户返回响应
    <span style="color: #800080;">$response</span>->send();

     

  1.4.2 App是如何处理请求的

<span style="color: #0000ff;">   public</span> <span style="color: #0000ff;">function</span> handle(MinorRequest <span style="color: #800080;">$minorRequest</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$minorResponse</span> = MinorResponse::<span style="color: #000000;">getInstance();

        </span><span style="color: #800080;">$this</span>->minorRequest = <span style="color: #800080;">$minorRequest</span><span style="color: #000000;">;

        </span><span style="color: #0000ff;">list</span>(<span style="color: #800080;">$controllerName</span>, <span style="color: #800080;">$actionName</span>, <span style="color: #800080;">$params</span>) = <span style="color: #800080;">$this</span>->router->dispatcher(<span style="color: #800080;">$minorRequest</span>-><span style="color: #000000;">getUrl());

        </span><span style="color: #0000ff;">if</span> (!(<span style="color: #800080;">$controllerName</span> && <span style="color: #800080;">$actionName</span> && <span style="color: #008080;">is_array</span>(<span style="color: #800080;">$params</span><span style="color: #000000;">))) {
            </span><span style="color: #0000ff;">if</span> (!Config::get(['app' => 'DEBUG']) && (<span style="color: #800080;">$handler</span> = Config::get(['app' => '404_HANDLER'])) && (<span style="color: #800080;">$handler</span><span style="color: #000000;"> instanceof \Closure)) {
                </span><span style="color: #800080;">$handler</span>(<span style="color: #800080;">$minorRequest</span>-><span style="color: #000000;">getRequestUrl());
            } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> RouteException('404 Not Found'<span style="color: #000000;">);
            }
        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            </span><span style="color: #800080;">$minorResponse</span> = <span style="color: #800080;">$this</span>->invoke(<span style="color: #800080;">$controllerName</span>, <span style="color: #800080;">$actionName</span>, <span style="color: #800080;">$params</span><span style="color: #000000;">);
        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$minorResponse</span><span style="color: #000000;">;
    }</span>

 

  1. 先初始化一个默认的响应对象
    <span style="color: #800080;">$minorResponse = MinorResponse::getInstance();</span>
  2. 调用Router的dispatcher($url)方法根据路由配置获取url对应的控制器和方法以及参数
    <span style="color: #0000ff;">list(<span style="color: #800080;">$controllerName, <span style="color: #800080;">$actionName, <span style="color: #800080;">$params) = <span style="color: #800080;">$this->router->dispatcher(<span style="color: #800080;">$minorRequest->getUrl());</span></span></span></span></span></span>

     

  3. 执行控制器的制定方法,并返回响应对象
    <span style="color: #800080;">$minorResponse = <span style="color: #800080;">$this->invoke(<span style="color: #800080;">$controllerName, <span style="color: #800080;">$actionName, <span style="color: #800080;">$params<span style="color: #000000;">);
    
    <span style="color: #0000ff;">return <span style="color: #800080;">$minorResponse;</span></span></span></span></span></span></span></span>

 

1.5 请忽视以上吹牛逼字段

  恩!就是请忽视。别怪我没提醒

  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn