本文整理汇总了PHP中Phalcon\Mvc\Dispatcher类的典型用法代码示例。如果您正苦于以下问题:PHP Dispatcher类的具体用法?PHP Dispatcher怎么用?PHP Dispatcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dispatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: registerServices
public function registerServices(DiInterface $di)
{
$di['dispatcher'] = function () {
$eventsManager = new \Phalcon\Events\Manager();
//Attach a listener
$eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
//controller or action doesn't exist
if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
$dispatcher->forward(array('controller' => 'error', 'action' => 'error404'));
return false;
}
switch ($exception->getCode()) {
case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array('controller' => 'error', 'action' => 'error404'));
return false;
}
});
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Modules\\Frontend\\Controllers");
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
};
$di['view'] = function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->setLayoutsDir('../../common/layout_frontend/');
return $view;
};
}
示例2: registerServices
/**
* Register specific services for the module
*/
public function registerServices(DiInterface $di)
{
$config = $di->get('config');
//Registering a dispatcher
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Promoziti\\Modules\\Business\\Controllers");
return $dispatcher;
});
$di->set('view', function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->registerEngines(array('.volt' => function ($view, $di) {
$volt = new VoltEngine($view, $di);
$config = $di->get('config');
$volt->setOptions(array('compileAlways' => true, 'compiledPath' => $config->application->cache_dir . "volt/", 'compiledSeparator' => '_'));
return $volt;
}));
return $view;
});
$di->set('aws_s3', function () use($config) {
//version 2.7 style
$s3 = \Aws\S3\S3Client::factory(array('key' => $config->application->security->aws->key, 'secret' => $config->application->security->aws->secret, 'region' => 'us-west-2', 'version' => '2006-03-01'));
return $s3;
});
}
示例3: registerServices
/**
* Register specific services for the module
*/
function registerServices(\Phalcon\DiInterface $di)
{
//Registering a dispatcher
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('Modules\\Frontend\\Controllers');
return $dispatcher;
});
$config = $di->getShared('config');
$di->set('view', function () use($config, $di) {
$view = new View();
$router = $di->getShared('router');
/*
* @todo 给layouts等目录统一变量
* */
$view->setViewsDir(__DIR__ . '/views/');
$view->setLayoutsDir('/../../../layouts/');
// 多模块的话, 可能会有多个风格,所以需要切换layout,这里的Path是根据当前module的Path向上走的
$view->setLayout('index');
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
}, true);
}
示例4: beforeExecuteRoute
/**
* @param Dispatcher $dispatcher
*/
public function beforeExecuteRoute(Dispatcher $dispatcher)
{
$controllerName = $dispatcher->getControllerName();
$actionName = $dispatcher->getActionName();
// This confirm a private zone
//check for a closed controller and Action is exist a current session
if ($this->acl->isClosed($controllerName, $actionName)) {
if (!is_null($this->auth->getAccess())) {
//This redirect to another Controller/Action
$this->response->redirect('dashboard');
// Disable the view to avoid rendering
$this->view->disable();
}
return true;
}
if ($this->acl->isPrivate($controllerName)) {
if (!is_null($this->auth->getAccess())) {
//echo "Logeado";
} else {
//Display a error by a flash component
$this->flash->notice('Upss! Access denied, Please Registry first or Login into Kangoo');
//Execute the dispatcher to move above the user
$dispatcher->forward(array('controller' => 'index', 'action' => 'index'));
return false;
}
}
}
示例5: beforeDispatch
/**
* This action is executed before execute any action in the application.
*
* @param PhalconEvent $event Event object.
* @param Dispatcher $dispatcher Dispatcher object.
*
* @return mixed
*/
public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher)
{
$di = $this->getDI();
$cookie = $di->getCookie();
$session = $di->getSession();
$config = $di->getConfig();
$languageCode = '';
if ($di->get('app')->isConsole()) {
return;
}
// Detect language from cookie
if ($cookie->has('languageCode')) {
$languageCode = $cookie->get('languageCode')->getValue();
} else {
// Get default language from language model
$languageCode = LanguageModel::findFirst(['default = :isdefault: AND status = :enable:', 'bind' => ['isdefault' => LanguageModel::IS_DEFAULT, 'enable' => LanguageModel::STATUS_ENABLE]])->code;
}
// Set language code to session
if ($session->has('languageCode') && $session->get('languageCode') != $languageCode || !$session->has('languageCode')) {
$session->set('languageCode', $languageCode);
}
$messages = [];
$directory = $di->get('registry')->directories->modules . ucfirst($dispatcher->getModuleName()) . '/Lang/' . $languageCode . '/' . strtolower($dispatcher->getControllerName());
$extension = '.php';
if (file_exists($directory . $extension)) {
require $directory . $extension;
}
// add default core lang package
require $di->get('registry')->directories->modules . self::DEFAULT_LANG_PACK . '/Lang/' . $languageCode . '/default.php';
$translate = new PhTranslateArray(['content' => array_merge($messages, $default)]);
$di->set('lang', $translate);
return !$event->isStopped();
}
示例6: registerServices
public function registerServices(DiInterface $di)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
$di['dispatcher'] = function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Modules\\Backend\\Controllers");
return $dispatcher;
};
/**
* Setting up the view component
*/
$di['view'] = function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->setLayoutsDir('../../common/layouts/backend/');
$view->setTemplateAfter('main');
return $view;
};
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
return new MySQLAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name));
};
}
示例7: registerServices
public function registerServices(\Phalcon\DiInterface $di = null)
{
/**
* Read configuration
*/
$config = (include dirname(dirname(dirname(__DIR__))) . "/apps/config/config.php");
$di->set('dispatcher', function () use($di) {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('Modules\\Frontend\\Controllers');
return $dispatcher;
}, true);
$di->set('view', function () use($config) {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, 'charset' => 'utf8'));
};
}
示例8: beforeExecuteRoute
public function beforeExecuteRoute(Event $event,Dispatcher $dispatcher){
//return;
//$this->session->destroy();
$role=$this->session->get('role');
if(!$role){
$role=self::GUEST;
}
//Get the current Controller & Action from the dispatcher
$controller=$dispatcher->getControllerName();
$action=$dispatcher->getActionName();
//Get the ACL rule list
$acl=$this->_getAcl();
//See if they have permission
$allowed=$acl->isAllowed($role, $controller,$action);
if($allowed!=Acl::ALLOW){
$this->flash->error('You Don\'t Have Permission To Access This Area');
$this->response->redirect('index');
//Stops the dispatcher at current operation
return false;
}
}
示例9: afterDispatchLoop
public function afterDispatchLoop(Event $event, Dispatcher $dispatcher)
{
$di = $this->getDI();
$response = $di->get('response');
$content = $response->getContent();
if ($content === '' && $dispatcher->getActiveController() instanceof RestControllerInterface) {
$returnedResponse = $dispatcher->getReturnedValue() instanceof ResponseInterface;
if ($returnedResponse === false) {
/** @var \PhalconRest\Mvc\RestView $rest */
$rest = $di->get('rest');
/** @var Manager $eventsManager */
$eventsManager = $this->_eventsManager;
//$eventsManager = $dispatcher->getDI()->get('eventsManager');
$renderStatus = true;
if ($eventsManager instanceof ManagerInterface) {
$renderStatus = $eventsManager->fire('application:viewRender', $this, $rest);
}
if ($renderStatus) {
$rest->render($dispatcher->getControllerName(), $dispatcher->getActionName());
$content = $rest->getContent();
}
/** @var \Phalcon\Http\Response $response */
$response = $di->get('response');
$response->setContent($content)->send();
}
}
}
示例10: beforeDispatch
/**
* This action is executed before execute any action in the application
*/
public function beforeDispatch(Event $event, Dispatcher $dispatcher)
{
if ($this->config->application->user_login_form_cookies) {
//use cookies
$auth = $this->_getCookie('auth');
if (!$auth) {
$role = 'Guests';
} else {
$role = $this->_getCookie('role');
$role = 'Person';
}
} else {
$auth = $this->session->get('auth');
$auth = $this->_getCookie('auth');
if (!$auth) {
$role = 'Guests';
} else {
$role = $auth['role'];
// $role='Common';
}
}
$controller = $dispatcher->getControllerName();
$action = $dispatcher->getActionName();
$acl = $this->getAcl();
$allowed = $acl->isAllowed($role, $controller, $action);
if ($allowed != Acl::ALLOW) {
$this->flash->error("You don't have access to this module");
$dispatcher->forward(array('controller' => 'user', 'action' => 'login'));
return false;
}
}
示例11: registerServices
/**
* Register specific services for the module
*/
public function registerServices(DiInterface $di)
{
// Assign our new tag a definition so we can call it
$di->set('Utilitarios', function () {
return new \Ecommerce\Admin\Helpers\UtilitariosHelper();
});
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('Ecommerce\\Admin\\Controllers');
return $dispatcher;
});
// Registering the view component
$di->set('view', function () {
$config = (include __DIR__ . "/config/config.php");
$view = new View();
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
$view->setViewsDir('../apps/admin/views/');
return $view;
});
include "../apps/admin/vendor/autoload.php";
}
示例12: afterExecuteRoute
/**
* This action is executed after execute any action in the application.
*
* @param PhalconEvent $event Event object.
* @param Dispatcher $dispatcher Dispatcher object.
*
* @return mixed
*/
public function afterExecuteRoute(PhEvent $event, Dispatcher $dispatcher)
{
$config = $this->getDI()->get('config')->toArray();
$controllerName = $dispatcher->getControllerName();
$actionName = $dispatcher->getActionName();
$this->getDI()->get('view')->pick($controllerName . '/' . $config['global']['template'][$controllerName] . '/' . $actionName);
}
示例13: registerServices
/**
* Registration services for specific module
* @param \Phalcon\DI $di
* @access public
* @return mixed
*/
public function registerServices($di)
{
// Dispatch register
$di->set('dispatcher', function () use($di) {
$eventsManager = $di->getShared('eventsManager');
$eventsManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(['module' => self::MODULE, 'namespace' => 'Modules\\' . self::MODULE . '\\Controllers\\', 'controller' => 'error', 'action' => 'notFound']);
return false;
break;
default:
$dispatcher->forward(['module' => self::MODULE, 'namespace' => 'Modules\\' . self::MODULE . '\\Controllers\\', 'controller' => 'error', 'action' => 'uncaughtException']);
return false;
break;
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setDefaultNamespace('Modules\\' . self::MODULE . '\\Controllers');
return $dispatcher;
}, true);
// Registration of component representations (Views)
$di->set('view', function () {
$view = new View();
$view->setViewsDir($this->_config['application']['viewsFront'])->setMainView('layout');
return $view;
});
return require_once APP_PATH . '/Modules/' . self::MODULE . '/config/services.php';
}
示例14: registerServices
/**
* Registers the module-only services
*
* @param \Phalcon\DiInterface $di
*/
public function registerServices($di)
{
/**
* Read application wide and module only configurations
*/
$appConfig = $di->get('config');
$moduleConfig = (include __DIR__ . '/config/config.php');
$di->set('moduleConfig', $moduleConfig);
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use($appConfig) {
$url = new UrlResolver();
$url->setBaseUri($appConfig->application->baseUri);
return $url;
});
/**
* Module specific dispatcher
*/
$di->set('dispatcher', function () use($di) {
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($di->getShared('eventsManager'));
$dispatcher->setDefaultNamespace('App\\Modules\\Oauth\\');
return $dispatcher;
});
/**
* Module specific database connection
*/
$di->set('db', function () use($appConfig) {
return new DbAdapter(['host' => $moduleConfig->database->host, 'username' => $moduleConfig->database->username, 'password' => $moduleConfig->database->password, 'dbname' => $moduleConfig->database->name]);
});
}
示例15: beforeException
public function beforeException(Event $event, Dispatcher $dispatcher, \Exception $e)
{
$this->getLogger()->exception($e);
$this->response->setStatusCode($e->getCode() ?: 500, $e->getMessage() ?: 'Application error');
$dispatcher->forward(['namespace' => 'Controller', 'controller' => 'error', 'action' => 'index', 'params' => [0 => $e->getMessage()]]);
return false;
}