public function _testRouterHttp() { $tests = array(array('method' => null, 'uri' => '/documentation/index/hello', 'controller' => 'documentation', 'action' => 'index', 'params' => array('hello')), array('method' => 'POST', 'uri' => '/docs/index', 'controller' => 'documentation3', 'action' => 'index', 'params' => array()), array('method' => 'GET', 'uri' => '/docs/index', 'controller' => 'documentation4', 'action' => 'index', 'params' => array()), array('method' => 'PUT', 'uri' => '/docs/index', 'controller' => 'documentation5', 'action' => 'index', 'params' => array()), array('method' => 'DELETE', 'uri' => '/docs/index', 'controller' => 'documentation6', 'action' => 'index', 'params' => array()), array('method' => 'OPTIONS', 'uri' => '/docs/index', 'controller' => 'documentation7', 'action' => 'index', 'params' => array()), array('method' => 'HEAD', 'uri' => '/docs/index', 'controller' => 'documentation8', 'action' => 'index', 'params' => array())); $di = new Phalcon\DI(); $di->set('request', function () { return new Phalcon\Http\Request(); }); $router = new Phalcon\Mvc\Router(); $router->setDI($di); $router->add('/docs/index', array('controller' => 'documentation2', 'action' => 'index')); $router->addPost('/docs/index', array('controller' => 'documentation3', 'action' => 'index')); $router->addGet('/docs/index', array('controller' => 'documentation4', 'action' => 'index')); $router->addPut('/docs/index', array('controller' => 'documentation5', 'action' => 'index')); $router->addDelete('/docs/index', array('controller' => 'documentation6', 'action' => 'index')); $router->addOptions('/docs/index', array('controller' => 'documentation7', 'action' => 'index')); $router->addHead('/docs/index', array('controller' => 'documentation8', 'action' => 'index')); foreach ($tests as $n => $test) { $_SERVER['REQUEST_METHOD'] = $test['method']; $this->_runTest($router, $test); } }
<?php $router = new Phalcon\Mvc\Router(); //Remove trailing slashes automatically $router->removeExtraSlashes(true); //SECURE ROUTES $router->add('/login', 'Secure::login'); $router->addGet('/sign-up', 'Secure::signUp'); $router->addGet('/remind-password', 'Secure::remindPassword'); $router->addGet('/logout', 'Secure::logout'); $router->addPost('/register', 'Secure::register'); $router->add('/plates/{id}', 'Requests::plates'); //testing porpuses $router->addGet('/test', 'Index::test'); //Requests section $router->addGet('/requests', 'Requests::index'); $router->addGet('/requests/history', 'Requests::history'); $router->addPost('/requests/create', 'Requests::create'); $router->addPost('/requests/view/{id}', 'Requests::view'); $router->addGet('/requests/status/{id}/{id_status}', 'Requests::changeStatus'); //Cart $router->addPost('/add-cart-item', 'Cart::add'); $router->addGet('/add-cart-item', 'Index::home'); $router->addGet('/cart', 'Cart::view'); $router->add('/cart-delete/{id}', 'Cart::delete'); $router->add('/delete-cart', 'Cart::deleteCart'); //Inbox section $router->addGet('/inbox', 'Inbox::index'); //Reports section $router->addGet('/reports', 'Reports::index'); $router->addGet('/reports/history', 'Reports::history');
/** * Start the session the first time some component request the session service */ $di->set('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); /** * Register configurations */ $di->set('config', function () use($config) { return $config; }); /** * Register router */ $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->removeExtraSlashes(true); $router->setDefaults(['controller' => 'index', 'action' => 'index']); /*$router->notFound([ 'controller' => 'errors', 'action' => 'pageNotFound' ]);*/ $router->addGet('/project/{profile:([a-zA-Z0-9-(.)]+)}', ['action' => 'profile', 'project' => 1]); $router->addPost('/filter', ['action' => 'filter'])->beforeMatch(function ($uri, $route) { return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; }); return $router; });
<?php $router = new \Phalcon\Mvc\Router(); // list users $router->addGet("/api/user", "Api::listUser"); // create user $router->addPost("/api/user", "Api::createUser"); // get user by id $router->addGet("/api/user/{id}", "Api::getUser"); // edit user by id $router->add("/api/user/{id}", "Api::editUser")->via(array("POST", "PUT")); // delete user by id $router->addDelete("/api/user/{id}", "Api::deleteUser"); return $router;
$year = $this->dispatcher->getParam("year"); $month = $this->dispatcher->getParam("month"); $day = $this->dispatcher->getParam("day"); //*/ $router->add("/docs/{chapter}/{name}.{type:[a-z]+}", array("controller" => "documentation", "action" => "show")); $router->add("/posts/{year:[0-9]+}/{title:[a-z\\-]+}", "Posts::show"); $router->add("/([a-z]{2})/:controller", array("controller" => 2, "action" => "index", "language" => 1)); $router->add("/{language:[a-z]{2}}/:controller", array("controller" => 2, "action" => "index")); $router->add("/manual/([a-z]{2})/([a-z\\.]+)\\.html", array("controller" => "manual", "action" => "show", "language" => 1, "file" => 2)); $router->add('/news/{country:[a-z]{2}}/([a-z+])/([a-z\\-+])', array('section' => 2, 'article' => 3)); $router->add("/admin/:controller/:action/:int", array("controller" => 1, "action" => 2, "id" => 3)); $router->add('/api/(v1|v2)/{method:[a-z]+}/{param:[a-z]+}\\.(json|xml)', array('controller' => 'api', 'version' => 1, 'format' => 4)); $router->add('/:module/:controller/:action/:params', array('module' => 1, 'controller' => 2, 'action' => 3, 'params' => 4)); // 限制 HTTP 请求传入方式(HTTP Method Restrictions) // This route only will be matched if the HTTP method is GET $router->addGet("/products/edit/{id}", "Products::edit"); // This route only will be matched if the HTTP method is POST $router->addPost("/products/save", "Products::save"); // This route will be matched if the HTTP method is POST or PUT $router->add("/products/update")->via(array("POST", "PUT")); // 使用转换(Using convertions) //The action name allows dashes, an action can be: /products/new-ipod-nano-4-generation $router->add('/products/{slug:[a-z\\-]+}', array('controller' => 'products', 'action' => 'show'))->convert('slug', function ($slug) { //Transform the slug removing the dashes return str_replace('-', '', $slug); }); // 路由分组(Groups of Routes) class BlogRoutes extends Phalcon\Mvc\Router\Group { public function initialize() {
}); /** * Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); /* * здесь надо добавить в контейнер свои экземпляры классов стандартных компонентов phalcon'а * или свои какие-то экземпляры класса, т.к. это просто реализация контейнера */ $di->setShared("view", function () { $view = new View(); $view->disable(); return $view; }); $di->setShared("router", function () { $router = new \Phalcon\Mvc\Router(false); $router->removeExtraSlashes(true); $router->setDefaultController('robots'); $router->addGet('/v1/api/robots', ['controller' => 'robots', 'action' => 'list']); $router->addGet('/v1/api/robots/{id:[0-9]+}', ['controller' => 'robots', 'action' => 'findOneById']); $router->addGet('/v1/api/robots/search/{name}', ['controller' => 'robots', 'action' => 'search']); $router->addPost('/v1/api/robots', ['controller' => 'robots', 'action' => 'create']); $router->addPut('/v1/api/robots/{id}', ['controller' => 'robots', 'action' => 'update']); $router->addDelete('/v1/api/robots/{id}', ['controller' => 'robots', 'action' => 'delete']); return $router; }); $di->set('config', $config);
* Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); $di->setShared("view", function () { $view = new View(); $view->disable(); return $view; }); $di->setShared("router", function () { $router = new \Phalcon\Mvc\Router(false); $router->removeExtraSlashes(true); $router->setDefaultController('products'); $router->addGet('/v1/api/products', ['controller' => 'products', 'action' => 'list']); $router->addGet('/v1/api/products/{id:[0-9]+}', ['controller' => 'products', 'action' => 'findOneById']); $router->addGet('/v1/api/products/search/{name}', ['controller' => 'products', 'action' => 'search']); $router->addPost('/v1/api/products', ['controller' => 'products', 'action' => 'create']); $router->addPut('/v1/api/products/{id}', ['controller' => 'products', 'action' => 'update']); $router->addDelete('/v1/api/products/{id}', ['controller' => 'products', 'action' => 'delete']); $router->addGet('/v1/api/categories', ['controller' => 'categories', 'action' => 'list']); $router->addGet('/v1/api/categories/{id:[0-9]+}', ['controller' => 'categories', 'action' => 'findOneById']); $router->addGet('/v1/api/categories/search/{name}', ['controller' => 'categories', 'action' => 'search']); $router->addPost('/v1/api/categories', ['controller' => 'categories', 'action' => 'create']); $router->addPut('/v1/api/categories/{id}', ['controller' => 'categories', 'action' => 'update']); $router->addDelete('/v1/api/categories/{id}', ['controller' => 'categories', 'action' => 'delete']); return $router; }); $di->set('config', $config);