Phalcon also uses dependency injection, and Phalcon in the process of booting, need to use a lot of classes, these classes are configured through dependency injection, and then loaded through the loader, the following describes the service often need to inject.
For router Router, it is responsible for parsing the URL parameters, the class file is \phalcon\mvc\router, its add parameter class configures a route, the following is an example of the addition:
$di->set (' Router ', function () { $router = new \phalcon\mvc\router (); $router->add ("/:module/:controller/:action/:p arams", Array ( ' module ' = = 1, ' controller ' = 2, ' Action ' = 3, ' params ' = 4, ' ); $router->setdefaultmodule ("entrance"); $router->setdefaultcontroller ("index"); $router->setdefaultaction ("index"); $router->notfound ( Array ( "module" = "entrance", "Controller" = "index", "action" = > "index", ) ); return $router; }
For view views, the typical example of our general configuration is as follows:
$di->set (' View ', function () { $view = new \phalcon\mvc\view (); $view->setbasepath ('./entrance/'); $view->setviewsdir ('./views/'); $view->registerengines (Array ( '. Volt ' = ' Phalcon\mvc\view\engine\volt ', ". phtml" = ' Phalcon\mvc \view\engine\php ', )); return $view;
For URLs, we need to add that its main purpose is to avoid problems when jumping, general configuration:
$di->set (' url ', function () { $url = new Phalcon\mvc\url (); $url->setbaseuri ('/star/'); return $url;
For the session, give an example:
$di->setshared (' Session ', function () { $session = new Phalcon\session\adapter\files (); $session->start (); return $session; });
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sinsing analysis of commonly used dependency injection in Phalcon