/** * apps以下のrouting設定に沿ってルーティングをする */ public function routing() { $nameSpace = APPS_MAIN_CONF['base_namespace'] . '\\Controllers\\'; foreach ($this->conf as $controller => $settings) { $class = $nameSpace . $controller . 'Controller'; $this->init(); $this->router->setHandler($class, true); $this->router->setPrefix($settings['prefix']); foreach ($settings['actions'] as $method => $actions) { $this->setAction($method, $actions); } $this->app->mount($this->router); } }
/** * Mount all collections */ public function mount() { $this->scanNamespaces(); foreach ($this->collections as $col) { $this->app->mount($col); } }
// } // // $usr = new Users(); // $auth['id'] = $usr->getUserId($auth['login'], $auth['password']); // // return $auth; //}; // CoreController if ($app['controllers']['core']) { $core = new MicroCollection(); // Set the handler & prefix $core->setHandler(new CoreController($app)); $core->setPrefix('/'); // Set routers $core->get('/', 'index'); $app->mount($core); } // UsersController if ($app['controllers']['user']) { $users = new MicroCollection(); // Set the handler & prefix $users->setHandler(new UserController($app)); $users->setPrefix('/user'); // Set routers $users->post('/', 'create'); $users->put('/{id}', 'update'); $users->delete('/{id}', 'delete'); $users->get('/', 'userList'); $users->get('/{id}', 'info'); $app->mount($users); }
include $config->app->baseDir . 'config/services.php'; /** * Create the application */ $app = new Micro($di); /* * Configure HTTP response */ $app->response->setHeader('Access-Control-Allow-Origin', '*'); $app->response->setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, Authorization'); /* * Add collections for routing */ $collections = glob($config->app->collectionsDir . '*.php'); foreach ($collections as $collection) { $app->mount(include_once $collection); } /** * Handle the request */ $app->notFound(function () use($app) { throw new Exception('Not Found', 404); }); $app->after(function () use($app) { $data = $app->getReturnedValue(); $app->response->setContentType('application/json', 'utf-8'); $app->response->setStatusCode($data['code'], null); $app->response->setJsonContent($data['content']); $app->response->send(); }); $app->handle();
$di->setShared('logger', function () { return new \Phalcon\Logger\Adapter\File(APP_PATH . '/logs/' . date('Ymd') . '.log'); }); // api 规则路由 $apiRegister = (require_once APP_PATH . '/config/register.php'); // 注册命名空间 $loader = new \Phalcon\Loader(); $loader->registerNamespaces($apiRegister['namespace'])->register(); foreach ($apiRegister['list'] as $class => $conf) { $index = new Micro\Collection(); $index->setHandler(new $class()); $index->setPrefix($conf['prefix']); foreach ($conf['router'] as $router) { $index->{$router}[0]($router[1], $router[2]); } $app->mount($index); } // 设置数据库连接 $di->set('db', function () use($config) { $config = $config->get('database')->toArray(); $dbAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter']; $connection = new $dbAdapter($config); return $connection; }); // 设置配置 $di->set('config', function () use($config) { return $config; }); // 未找到配置 $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders();
try { //Initialize Dependency Injection $di = new Phalcon\DI\FactoryDefault(); //Initialize DB include_once __DIR__ . '/app/config/database.php'; //Register Directories $loader = new \Phalcon\Loader(); $loader->registerDirs(array(__DIR__ . '/app/models/', __DIR__ . '/app/controllers/', __DIR__ . '/app/classes/'))->register(); // Use composer autoloader to load vendor classes require_once __DIR__ . '/vendor/autoload.php'; //Create the app $app = new Micro(); // Mount the routes $routes = (include_once __DIR__ . '/app/config/routes.php'); foreach ($routes as $route) { $app->mount($route); } // Default Response $app->get('/', function () { return Rs::p(1, 'API is up!'); }); //Add any filter before running the route $app->before(function () use($app) { //You may want to add some basic auth in order to access the REST API }); //This is executed after running the route $app->after(function () use($app) { }); // Not Found $app->notFound(function () use($app) { return Rs::p(0, 'Not Found', [], 404);
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql; // Use Loader() to autoload our model $loader = new Loader(); $loader->registerDirs(array('models', 'controllers'))->register(); $loader->setExtensions(array("php", "inc", "phb")); $di = new FactoryDefault(); // Set up the database service $di->set('db', function () { return new PdoMysql(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "oversign")); }); $app = new Micro($di); $users = new MicroCollection(); $users->setHandler("UserController", true); $users->setPrefix('/user'); $users->get('/', 'hello'); $app->mount($users); // Retrieves all robots $app->get('/api/robots', function () { $phql = "SELECT * FROM Robots ORDER BY name"; $robots = $app->modelsManager->executeQuery($phql); $data = array(); foreach ($robots as $robot) { $data[] = array('id' => $robot->id, 'name' => $robot->name); } echo json_encode($data); }); // Searches for robots with $name in their name $app->get('/api/robots/search/{name}', function ($name) { $phql = "SELECT * FROM Robots WHERE name LIKE :name: ORDER BY name"; $robots = $app->modelsManager->executeQuery($phql, array('name' => '%' . $name . '%')); $data = array();
$auth['login'] = null; $auth['password'] = null; } $usr = new Users(); $auth['id'] = $usr->getUserId($auth['login'], $auth['password']); return $auth; }; // CoreController if ($app['controllers']['core']) { $core = new MicroCollection(); // Set the handler & prefix $core->setHandler(new CoreController($app)); $core->setPrefix('/'); // Set routers $core->get('/', 'index'); $app->mount($core); } // MessagesController if ($app['controllers']['messages']) { $messages = new MicroCollection(); // Set the handler & prefix $messages->setHandler(new MessagesController($app)); $messages->setPrefix('/messages'); // Set routers $messages->post('/', 'create'); $messages->get('/{id_sender}/{id_receiver}', 'stream'); $messages->get('/', 'inbox'); $app->mount($messages); } // UsersController if ($app['controllers']['users']) {
<?php use Phalcon\Mvc\Micro; use Phalcon\Mvc\Micro\Collection; define('VERSION', 'v1'); define('ROOT_DIR', dirname(__FILE__)); define('DS', DIRECTORY_SEPARATOR); try { include ROOT_DIR . '/config/loader.php'; include ROOT_DIR . '/config/service.php'; require ROOT_DIR . '/vendor/autoload.php'; $app = new Micro($di); $app->setDI($di); $app->before(function () use($app, $di) { }); foreach ($di->get('collections') as $collection) { $app->mount($collection); } $app->handle(); } catch (Exception $e) { echo $e->getMessage(); d($e->getTraceAsString()); }