Learning SlimFrameworkforPHPv3 (1)

Source: Internet
Author: User
Learning SlimFrameworkforPHPv3 (1) because the company's project uses the slim framework, I want to learn about it. Slim2 is used in the company, and the official website has reached the Slim3 version. Official Website address: www. cnblogs. comlmenglliren89php. First, follow the tutorials on the official website to install Slim: 1. curl-sShttp to learn about Slim Framework for PHP v3 (1)

Because the company's projects use the slim framework, I want to learn about it. Slim2 is used in the company, and the official website has reached the Slim3 version. Official Website address: http://www.cnblogs.com/lmenglliren89php /.

First, follow the tutorials on the official website to 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"

This Slim is installed. Apache's DirectDocumentroot settings are also available:AllowOverride All。

At the same time, its built-in Example can also be run. you need to adjust the folder location.

  

  How can we understand a framework? Is it easy to use? Or is it a step-by-step process?

  My idea is like this. I changed this Example to my own project, and I did not know how to dig it into it when I did not know how to proceed. I do not know whether this logic is correct, do this for the moment.

The project logic requirements are as follows: submit data on the page ---> receive data ---> store data to the database ---> Log in ---> return the data written successfully

Route for receiving data:

    

$app->get('/replace/', function ($request, $response, $args) {         Example\Module\Replace::instance()->setBody($request, $response);    });

  

Slim is based on the concept of route. it transfers all requests to different route, then each route completes the function, and finally sets the response to complete the request.

The get method is to set a route, and the future 'replace 'will match the closure function.

  

Where is this route? There is a contianer in APP. php. What is this container? let's look at the code:

  

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;}

Let's see how Container works:

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);        };    }}

We can see that this code is used to initialize the router key in the container, and the later route will be added to this.

$ This ['router '] = function () {return new router ;};
 
 
But can I not add my own Key?

Write for the first time.

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.