Learn slim Framework for PHP v3 (iv)

Source: Internet
Author: User
Tags rfc

Look at the official website bold sentence:

At it core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and return s an HTTP response. That ' s it. So how does it distribute the received request and study it these days? first look at what needs to be done to Apache in order to get the request into index.php. The configuration is usually configured as follows: # directoryindex:sets The file that Apache would serve if a directory# is Requested.#<ifmodule dir_module> #Direc Toryindex index.html # XAMPP directoryindex index.html index.html.var index.php index.php3 INDEX.PHP4&LT;/IFMODULE&G Tguaranteed access to the directory is directly to index.xxx. There is a . Htaccesss under the project, the function of this file is to determine the URL of the request satisfies the condition no, that is, whether it satisfies Rewritecond of the match. Finally, if the condition is met then execute the rewriterule content, here let it jump to index.php.   
$app->get ('/forbase ', function ($request, $response, $args) {    example\module\base::instance ()->init ($ request, $response);    return $response;}) ->add (Example\middleware\mymiddleware::instance (Example\module\base::instance ()));

This article first solves: How get is added in. In the subsequent article, we resolved in turn: How the route was called, and how the route middleware was added.

  There are variable container in the app.php. The type of this variable is Slim\container, and Slim\container inherit Pimplecontainer, according to the official website said this pimple is a di Container, this is not understood.

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;    }
Class Container extends Pimplecontainer implements containerinterface{    /**     * Create new Container     *     * @ param array $values the parameters or objects.     */Public    function __construct (array $values = [])    {        parent::__construct ($values);        $userSettings = Isset ($values [' Settings '])? $values [' Settings ']: [];        $this->registerdefaultservices ($userSettings);    }

The associated defaultservices including router are registered in the container constructor method, and all subsequent route are present in the router:

  

if (!isset ($this [' router ')) {            /**             * This service must return a SHARED instance             * of \slim\interfaces\routeri Nterface.             *             * @return routerinterface             *            /$this [' router '] = function () {                return new router;            };        }

  This $this[' router ' is actually called the Offsetset () method in Pimple\container, because Pimple\container implements Arrayaccess, so $this[' router ' ] is available.

Public Function Offsetset ($id, $value) {    if (isset ($this->frozen[$id])) {        throw new \runtimeexception ( sprintf (' cannot override frozen service '%s '. ', $id));    }    $this->values[$id] = $value;    $this->keys[$id] = true;}

All of these default settings are stored in the values variable, so you can get the settings from the values later.

  At App->get (), the app class does the work:

a Get     ()/post () function accepts the route creation request. Two calls App->map (), three calls to Router->map (), and four sets the container and output buffer of the route.

On the code:

/** * ADD GET Route * * @param  string $pattern The  route URI pattern * @param  mixed  $callable the route Callback Routine * * @return \slim\interfaces\routeinterface */public function Get ($pattern, $callable) {    return $ This->map ([' GET '], $pattern, $callable);}
Public function Map (array $methods, $pattern, $callable) {    if ($callable instanceof Closure) {        $callable = $callab Le->bindto ($this->container);    }    $route = $this->container->get (' router ')->map ($methods, $pattern, $callable);    if (is_callable ([$route, ' Setcontainer ')) {        $route->setcontainer ($this->container);    }    if (is_callable ([$route, ' setoutputbuffering ')) {        $route->setoutputbuffering ($this->container->get ( ' Settings ') [' outputbuffering ']);    }    return $route;}

At the same time the work done by the Router class:

   when a GET, post, or other need to be added, a new route is generated in the map () method in router and each route is numbered, and the route is added to the router routes array.

On the code:

/** * Add Route * * @param string[] $methods Array of HTTP methods * @param string $pattern the route pattern * @param callable $handler the route callable * * @return routeinterface * * @throws Invalida Rgumentexception if the route pattern isn ' t a string */public function map ($methods, $pattern, $handler) {if (!is_strin    G ($pattern)) {throw new InvalidArgumentException (' Route pattern must be a string '); }//Prepend parent group pattern (s) if ($this->routegroups) {$pattern = $this->processgroups (). $pat    Tern    }//According to RFC methods is defined in uppercase (see RFC 7231) $methods = Array_map ("Strtoupper", $methods);    Add route $route = new Route ($methods, $pattern, $handler, $this->routegroups, $this->routecounter);    $this->routes[$route->getidentifier ()] = $route;    $this->routecounter++; return $route;} 

  After such a process, an added route is put into the routes array of router. After the app receives the request, it will select the appropriate route in routes to process the request.

  

  

Learn slim Framework for PHP v3 (iv)

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.