Learn slim Framework for PHP v3 (i)

Source: Internet
Author: User

Because the company's project uses the SLIM framework, so want to learn it. Used in the company is the SLIM2 version, now the official website has reached the SLIM3 version. Official address: http://www.cnblogs.com/lmenglliren89php/.

First, according to the official website of the tutorial, install Slim:

1.curl-ss Https://getcomposer.org/installer | sudo php----install-dir=/usr/local/bin--filename=composer

2.composer require Slim/slim "^3.0"

Such a slim is installed. and Apache's directdocumentroot settings:AllowOverride All。

At the same time its own example of example can also be run, you need to adjust the location of the folder is good.

  

  How can a framework be understood? Is it very smooth to use? Or are you going to follow it in a step?

  My idea is this, I changed this example to my own project, and then do not know how to go deep digging, do not know whether such logic is correct, for the time being do it.

The project logic requirement is that the page submission data--->> receive data--->> deposit into the database--->> log--->> return write success

Route to receive data:

    
$app->get ('/replace/', function ($request, $response, $args) {   example\module\replace::instance ()->setbod    Y ($request, $response); });

  

To say that Slim is based on the concept of the route, it passes all requests to different route, then each route completes the function, finally sets the response, the request is complete.    The Get method is to set a route, and the subsequent ' replace ' will be matched to that closure function. And where is this route placed? There is a contianer in app.php. What is this container, to see the code:
 Public function__construct ($container= []){    if(Is_array($container)) {        $container=NewContainer ($container); }    if(!$containerinstanceof Containerinterface) {        Throw NewInvalidArgumentException (' expected a containerinterface '); }    $this->container =$container;}

Let's see what container did:

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 a 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\int         Erfaces\http\environmentinterface. * * @return Environmentinterface */$this [' environment '] = function () {return new Envi        Ronment ($_server);    }; } if (!isset ($this [' request ')]) {/** * PSR-7 request objECT * * @param Container $c * * @return serverrequestinterface * * $this [' re        Quest '] = 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 = n            EW Headers ([' content-type ' = ' text/html; Charset=utf-8 ']);            $response = new Response ($headers);        Return $response->withprotocolversion ($c->get (' Settings ') [' httpversion ']);    }; } if (!isset ($this [' router ')) {/** * This service must return a SHARED instance * of \slim\int         Erfaces\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\responseint         Erface. * * @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 II arguments: * * 1. I Nstance 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 ACC Epts 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\RESP         Onseinterface. * * @return Callable */$this [' notallowedhandler '] = FuNction () {return new notallowed;    };         } if (!isset ($this [' callableresolver ')) {/** * Instance of \slim\interfaces\callableresolverinterface * * @param Container $c * * @return callableresolverinterface * * $this [' cal        Lableresolver '] = function ($c) {return new callableresolver ($c);    }; }}

will see this code is initialized container in the router key, the future route will be added to this inside is good.

$this [' router '] = function () {return new router;};
 
 
Can you just not join your own key?
For the first time, let's do it.

Learn slim Framework for PHP v3 (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.