由於公司的項目用到是slim 框架,因此想把它學習一下。在公司用到是Slim2版本,如今官網已經到達 Slim3的版本了。官網地址:http://www.cnblogs.com/lmenglliren89php/。php
首先按照官網的教程,安裝Slim:html
1.curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer數據庫
2.composer require slim/slim "^3.0"閉包
這樣一個Slim就安裝好了。還有Apache的DirectDocumentroot設置:AllowOverride All。
app
同時它自帶的Example的例子也是能夠運行的,須要調整下文件夾的位置就好。composer
如何才能理解一個框架呢?是很順利的使用嗎?仍是從一步步去跟進去它的流程?框架
個人思路是這樣的,我把這個Example改爲本身的項目,而後在不知道如何去的時候去深挖一下,不知道這樣的邏輯是否正確,暫且就這樣作吧。curl
項目邏輯要求是這樣的,頁面提交數據--->>接收數據--->>存入數據庫--->>記入日誌--->>返回寫入成功ide
接收數據的route:函數
$app->get('/replace/', function ($request, $response, $args) { Example\Module\Replace::instance()->setBody($request, $response); });
public function __construct($container = []) { if (is_array($container)) { $container = new Container($container); } if (!$container instanceof ContainerInterface) { throw new InvalidArgumentException('Expected a ContainerInterface'); } $this->container = $container; }
來看Container怎麼作的:
public function __construct(array $values = []) { parent::__construct($values); $userSettings = isset($values['settings']) ? $values['settings'] : []; $this->registerDefaultServices($userSettings); } private function registerDefaultServices($userSettings) { $defaultSettings = $this->defaultSettings; /** * This service MUST return an array or an * instance of \ArrayAccess. * * @return array|\ArrayAccess */ $this['settings'] = function () use ($userSettings, $defaultSettings) { return new Collection(array_merge($defaultSettings, $userSettings)); }; if (!isset($this['environment'])) { /** * This service MUST return a shared instance * of \Slim\Interfaces\Http\EnvironmentInterface. * * @return EnvironmentInterface */ $this['environment'] = function () { return new Environment($_SERVER); }; } if (!isset($this['request'])) { /** * PSR-7 Request object * * @param Container $c * * @return ServerRequestInterface */ $this['request'] = function ($c) { return Request::createFromEnvironment($c->get('environment')); }; } if (!isset($this['response'])) { /** * PSR-7 Response object * * @param Container $c * * @return ResponseInterface */ $this['response'] = function ($c) { $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']); $response = new Response(200, $headers); return $response->withProtocolVersion($c->get('settings')['httpVersion']); }; } if (!isset($this['router'])) { /** * This service MUST return a SHARED instance * of \Slim\Interfaces\RouterInterface. * * @return RouterInterface */ $this['router'] = function () { return new Router; }; } if (!isset($this['foundHandler'])) { /** * This service MUST return a SHARED instance * of \Slim\Interfaces\InvocationStrategyInterface. * * @return InvocationStrategyInterface */ $this['foundHandler'] = function () { return new RequestResponse; }; } if (!isset($this['errorHandler'])) { /** * This service MUST return a callable * that accepts three arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * 3. Instance of \Exception * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @param Container $c * * @return callable */ $this['errorHandler'] = function ($c) { return new Error($c->get('settings')['displayErrorDetails']); }; } if (!isset($this['notFoundHandler'])) { /** * This service MUST return a callable * that accepts two arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @return callable */ $this['notFoundHandler'] = function () { return new NotFound; }; } if (!isset($this['notAllowedHandler'])) { /** * This service MUST return a callable * that accepts three arguments: * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Psr\Http\Message\ResponseInterface * 3. Array of allowed HTTP methods * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * * @return callable */ $this['notAllowedHandler'] = function () { return new NotAllowed; }; } if (!isset($this['callableResolver'])) { /** * Instance of \Slim\Interfaces\CallableResolverInterface * * @param Container $c * * @return CallableResolverInterface */ $this['callableResolver'] = function ($c) { return new CallableResolver($c); }; } }
會看到這段代碼就是初始化container中的router key,之後的route都加到這個裏面就行了。
$this['router'] = function () { return new Router; };
只是能不加入本身的Key呢?