框架Github地址:github.com/Orlion/Minorphp
(若是以爲還不錯給個star哦(^-^)V)html
框架做者: Orlionlaravel
知乎:https://www.zhihu.com/people/orliongit
Github: https://github.com/Orliongithub
漂亮的URL絕對是一個嚴肅的web應用程序必須作到的,這種方式使index.php?article_id=57這類的醜陋URL被隱藏,由更受歡迎的像 /read/intro-to-symfony 來替代。正則表達式
配置文件爲app/Config/routes.php框架
<?php return [ '/demo/{productName}' => [ 'name' => 'test1', 'controller' => 'App\Modules\Demo\Controller\FooController', 'action' => 'bar', 'required' => ['productName' => '\w+'], ], ];
以上邊代碼爲例詳述一下如何配置一個優雅(laravel病上身...)的路由函數
'/demo/{productName}'是路由的匹配規則,其中配置中的required制定的就是productName必須知足的正則條件,在框架的實際運行過程當中會將路由規則+required解析爲正則表達式/demo/(\w+) ,當url(eg: xxx.xxx.xxx/demo/testproduct)匹配到這條正則時就會執行配置中的controller的action,具體的執行爲:FooController->bar($productName);
能夠看到路由規則中的大括號的內容(productName)就是就是bar這個方法的參數。因此在匹配路由時必定要注意路由規則必須與具體的控制器的方法的參數個數是一致的,否則就會拋出ControllerException。
若是咱們每定義一個Controller就要配置一個路由就會致使開發效率較低,爲了防止這種問題發生Minor提供了默認路由機制。當咱們訪問http://xxx.xxx.xxx/demo/foo/bar時就會執行App\Modules\Demo\FooController的bar方法,即默認路由爲:
http://xxx.xxx.xxx/{模塊名}/{控制器名}/{方法名}
正如你所見,Minor的路由並不強大。不支持請求方法的限制,不支持htttps限制,不支持過濾器。
Controller的定義很是簡單,只要繼承Minor\Controller\Controller基礎類就能夠了(固然你也能夠不繼承,可是基類中的方法和屬性就不能使用了,這很好理解),首先建立文件夾app/Modules/Demo/Controller/,而後再建立文件FooController.php:
<?php namespace App\Modules\Demo\Controller; use Minor\Controller\Controller; // 定義一個控制器 class FooController extends Controller { // 定義一個方法 public function bar() { return 'Hello World'; } }
經過訪問xxx.xxx.xxx/demo/foo/bar(默認路由,你也能夠配置本身的路由)就可看到返回了Hello World。
調用Url的gen方法能夠將默認路徑轉爲符合路由規則的url
$url = Url:gen($path);
如根據3.1.1中的路由配置Url::gen('/demo/foo/bar?productName=test') 將返回 /demo/test。
Minor提供了三個跳轉的方法分別是redirect、forward、forwardUrl(這三個都是Minor\Controller\Controller的protected方法)。
當跳轉到另一個url時能夠在控制器這樣調用:$this->redirect($url);
當轉向(froward)到另一個url時能夠在控制器中這樣調用: $this->forwardUrl($url);(該方法的實現其實就是經過路由解析出url請求的控制器和方法而後調用forward($controller, $action, $param))
當轉向(forward)到另一個方法時能夠在控制器中這樣調用: $this->forward($controller, $action, $params); (參數$controller是控制器的類名,包含命名空間)
例:
class FooController extends Controller { public function bar() { $this->redirect('www.baidu.com'); return $this->forward('App\Modules\Demo\Controller\FooController', 'bar', 'test'); return $this->forward('/demo/testpro'); } }
調用MinorRequest的get($paramName, $defaultParamValue = null) 或者 post($paramName, $defaultParamValue = null)方法就能夠獲取請求方法,在控制器中能夠這樣調用:
class FooController extends Controller { public function bar() { $minorRequest = $this->app->getMinorRequest();
$paramValue = $minorRequest->get('paramKey', 'defaultValue');
... } }
調用MinorRequest的getMethod()方法就能夠獲取請求的方法:
class FooController extends Controller { public function bar() { $minorRequest = $this->app->getMinorRequest(); $method = $minorRequest->getMethod(); ... } }
Minor提供了一個極其強大的模板引擎,這個模板引擎的名稱就是: PHP。是的!你沒看錯就是PHP。爲何Minor不提供一個相似於smarty或者Twig這樣的模板引擎呢? 由於沒有必要,PHP自己已經足夠好了,若是Minor再造一個模板引擎無疑就會使Minor更難上手,因此Minor直接使用PHP做爲視圖文件的語言。
在控制器中使用視圖只須要調用View::render('模塊名:控制器名:視圖文件名', ['param1key' => 'param1value', 'param2key' => 'param2value' ...]);例:
class FooController extends Controller { public function bar() { $param1 = 'Hello'; $param2 = 'World'; return View::render('Demo:Foo:bar.php', ['param1' => $param1, 'param2' => $param2]); } }
render函數的第二個參數(['param1key' => 'param1value', 'param2key' => 'param2value' ...])就是向視圖文件中傳遞的變量,咱們能夠在視圖文件中使用這些變量:
文件:app/Modules/Demo/Controller/Tpl/Foo/bar.php
<?php echo $param1key;?>
<?=$param2key ?>
Minor提供了兩個視圖文件中可使用的函數:
function include_tpl($module, $controller, $tpl) { require_once (!defined('APP_DIR') ? APP_DIR : realpath(__DIR__ . '/../../app/') .DIRECTORY_SEPARATOR) . 'Modules' . '/' . $module . '/Tpl/' . $controller . '/' . $tpl; } function url($path) { return Url::gen($path); }
使用:
html> <?php include_tpl('Public', 'Public', 'header.php');?> <body> <h1>Hello!</h1> <a href="<?php url('/demo/foo/bar?productName=testpro');?>"> </body> </html>
這兩個函數定義在app/Resource/functions.php文件中,你能夠在這個文件中自定義你須要的視圖函數。
能夠在控制器中經過調用App對象的getMinorResponse()方法來獲取當前MinorResponse對象。 MinorResponse類提供了六個方法分別是:
public function send(); // 用於將響應對象發送給客戶端
public function setHeader($header);// 設置響應頭
public function setContent($content); // 設置響應對象的內容
public function beforeContent($content); // 在當前已有的內容以前添加內容
public function appendContent($content); // 在當前已有內容以後追加內容
public function getContent(); // 獲取對象中的響應內容