<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # MVC 應用(MVC Applications) # MVC 應用(MVC Applications) 在Phalcon,策劃MVC操作背后的全部困難工作通常都可以通過 [*Phalcon\\Mvc\\Application*](#) 做到。這個組件封裝了全部后端所需要的復雜操作,實例化每一個需要用到的組件并與項目整合在一起,從而使得MVC模式可以如期地運行。 ### 單模塊或多模塊應用(Single or Multi Module Applications) 通過這個組件,你可以運行各式各樣的MVC結構: ### 單模塊(Single Module) 單一的MVC應用僅僅包含了一個模塊。可以使用命名空間,但不是必需的。這樣類型的應用可能會有以下文件目錄結構: ``` <pre class="calibre14">``` single/ app/ controllers/ models/ views/ public/ css/ img/ js/ ``` ``` 如果未使用命名空間,以下的啟動文件可用于編排MVC工作流: ``` <pre class="calibre14">``` <?php use Phalcon\Loader; use Phalcon\Mvc\View; use Phalcon\Mvc\Application; use Phalcon\DI\FactoryDefault; $loader = new Loader(); $loader->registerDirs( array( '../apps/controllers/', '../apps/models/' ) )->register(); $di = new FactoryDefault(); // 注冊視圖組件 $di->set('view', function () { $view = new View(); $view->setViewsDir('../apps/views/'); return $view; }); try { $application = new Application($di); echo $application->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); } ``` ``` 如果使用了命名空間,則可以使用以下啟動文件(譯者注:主要區別在于使用$loader的方式): ``` <pre class="calibre14">``` <?php use Phalcon\Loader; use Phalcon\Mvc\View; use Phalcon\Mvc\Dispatcher; use Phalcon\Mvc\Application; use Phalcon\DI\FactoryDefault; $loader = new Loader(); // 根據命名空間前綴加載 $loader->registerNamespaces( array( 'Single\Controllers' => '../apps/controllers/', 'Single\Models' => '../apps/models/', ) )->register(); $di = new FactoryDefault(); // 注冊調度器,并設置控制器的默認命名空間 $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Single\Controllers'); return $dispatcher; }); // 注冊視圖組件 $di->set('view', function () { $view = new View(); $view->setViewsDir('../apps/views/'); return $view; }); try { $application = new Application($di); echo $application->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); } ``` ``` ### 多模塊(Multi Module) 多模塊的應用使用了相同的文檔根目錄但擁有多個模塊。在這種情況下,可以使用以下的文件目錄結構: ``` <pre class="calibre14">``` multiple/ apps/ frontend/ controllers/ models/ views/ Module.php backend/ controllers/ models/ views/ Module.php public/ css/ img/ js/ ``` ``` 在apps/下的每一個目錄都有自己的MVC結構。Module.php文件代表了各個模塊不同的配置,如自動加載器和自定義服務: ``` <pre class="calibre14">``` <?php namespace Multiple\Backend; use Phalcon\Loader; use Phalcon\Mvc\View; use Phalcon\Mvc\Dispatcher; use Phalcon\Mvc\ModuleDefinitionInterface; class Module implements ModuleDefinitionInterface { /** * 注冊自定義加載器 */ public function registerAutoloaders() { $loader = new Loader(); $loader->registerNamespaces( array( 'Multiple\Backend\Controllers' => '../apps/backend/controllers/', 'Multiple\Backend\Models' => '../apps/backend/models/', ) ); $loader->register(); } /** * 注冊自定義服務 */ public function registerServices($di) { // Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Multiple\Backend\Controllers"); return $dispatcher; }); // Registering the view component $di->set('view', function () { $view = new View(); $view->setViewsDir('../apps/backend/views/'); return $view; }); } } ``` ``` 還需要一個指定的啟動文件來加載多模塊的MVC架構: ``` <pre class="calibre14">``` <?php use Phalcon\Mvc\Router; use Phalcon\Mvc\Application; use Phalcon\DI\FactoryDefault; $di = new FactoryDefault(); // 自定義路由 $di->set('router', function () { $router = new Router(); $router->setDefaultModule("frontend"); $router->add("/login", array( 'module' => 'backend', 'controller' => 'login', 'action' => 'index', )); $router->add("/admin/products/:action", array( 'module' => 'backend', 'controller' => 'products', 'action' => 1, )); $router->add("/products/:action", array( 'controller' => 'products', 'action' => 1, )); return $router; }); try { // 創建應用 $application = new Application($di); // 注冊模塊 $application->registerModules( array( 'frontend' => array( 'className' => 'Multiple\Frontend\Module', 'path' => '../apps/frontend/Module.php', ), 'backend' => array( 'className' => 'Multiple\Backend\Module', 'path' => '../apps/backend/Module.php', ) ) ); // 處理請求 echo $application->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); } ``` ``` 如果你想在啟動文件保持模塊的配置,你可以使用匿名函數來注冊對應的模塊: ``` <pre class="calibre14">``` <?php use Phalcon\Mvc\View; // 創建視圖組件 $view = new View(); // 設置視圖組件相關選項 // ... // Register the installed modules $application->registerModules( array( 'frontend' => function ($di) use ($view) { $di->setShared('view', function () use ($view) { $view->setViewsDir('../apps/frontend/views/'); return $view; }); }, 'backend' => function ($di) use ($view) { $di->setShared('view', function () use ($view) { $view->setViewsDir('../apps/backend/views/'); return $view; }); } ) ); ``` ``` 當 [*Phalcon\\Mvc\\Application*](#) 有多個模塊注冊時,通常每個都是需要的,以便每一個被匹配到的路由都能返回一個有效的模塊。每個已經注冊的模塊都有一個相關的類來提供建立和啟動自身的函數。而每個模塊定義的類都必須實現registerAutoloaders()和registerServices()這兩個方法,這兩個函數會在模塊即被執行時被 > [*Phalcon\\Mvc\\Application*](#) 調用。 ### 理解默認行為(Understanding the default behavior) 如果你已經看過了 [*tutorial*](#) 或者已經通過 [*Phalcon Devtools*](#) 生成了代碼,你將很容易識別以下的啟動文件: ``` <pre class="calibre14">``` <?php try { // 注冊自動加載器 // ... // 注冊服務 // ... // 處理請求 $application = new \Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch (\Exception $e) { echo "Exception: ", $e->getMessage(); } ``` ``` 控制器中全部核心的工作都會在handle()被回調時觸發執行。 ``` <pre class="calibre14">``` <?php echo $application->handle()->getContent(); ``` ``` ### 手動啟動(Manual bootstrapping) 如果你不想使用 [*Phalcon\\Mvc\\Application*](#) ,以上的代碼可以改成這樣: ``` <pre class="calibre14">``` <?php // 獲取 'router' 服務 $router = $di['router']; $router->handle(); $view = $di['view']; $dispatcher = $di['dispatcher']; // 傳遞路由的相關數據傳遞給調度器 $dispatcher->setControllerName($router->getControllerName()); $dispatcher->setActionName($router->getActionName()); $dispatcher->setParams($router->getParams()); // 啟動視圖 $view->start(); // 請求調度 $dispatcher->dispatch(); // 渲染相關視圖 $view->render( $dispatcher->getControllerName(), $dispatcher->getActionName(), $dispatcher->getParams() ); // 完成視圖 $view->finish(); $response = $di['response']; // 傳遞視圖內容給響應對象 $response->setContent($view->getContent()); // 發送頭信息 $response->sendHeaders(); // 輸出響應內容 echo $response->getContent(); ``` ``` 以下代碼替換了 [*Phalcon\\Mvc\\Application*](#) ,雖然缺少了視圖組件,但卻更適合Rest風格的API接口: ``` <pre class="calibre14">``` <?php // 獲取 'router' 服務 $router = $di['router']; $router->handle(); $dispatcher = $di['dispatcher']; // 傳遞路由的相關數據傳遞給調度器 $dispatcher->setControllerName($router->getControllerName()); $dispatcher->setActionName($router->getActionName()); $dispatcher->setParams($router->getParams()); // 請求調度 $dispatcher->dispatch(); // 獲取最后的返回結果 $response = $dispatcher->getReturnedValue(); // 判斷結果是否是 'response' 對象 if ($response instanceof Phalcon\Http\ResponseInterface) { // 發送響應 $response->send(); } ``` ``` 另外一個修改就是在分發器中對拋出異常的捕捉可以將請求轉發到其他的操作: ``` <pre class="calibre14">``` <?php // 獲取 'router' 服務 $router = $di['router']; $router->handle(); $dispatcher = $di['dispatcher']; // 傳遞路由的相關數據傳遞給調度器 $dispatcher->setControllerName($router->getControllerName()); $dispatcher->setActionName($router->getActionName()); $dispatcher->setParams($router->getParams()); try { // 請求調度 $dispatcher->dispatch(); } catch (Exception $e) { // An exception has occurred, dispatch some controller/action aimed for that // Pass the processed router parameters to the dispatcher $dispatcher->setControllerName('errors'); $dispatcher->setActionName('action503'); // Dispatch the request $dispatcher->dispatch(); } // 獲取最后的返回結果 $response = $dispatcher->getReturnedValue(); // 判斷結果是否是 'response' 對象 if ($response instanceof Phalcon\Http\ResponseInterface) { // 發送響應 $response->send(); } ``` ``` 盡管上面的代碼比使用 [*Phalcon\\Mvc\\Application*](#) 而需要的代碼遠遠要累贅得很,但它為啟動你的應用提供了一個可修改、可定制化的途徑。因為根據你的項目需要,你可以想對實例什么和不實例化什么進行完全的控制,或者想用你自己的組件來替代那些確定和必須的組件從而擴展默認的功能。 ### 應用事件(Application Events) [*Phalcon\\Mvc\\Application*](#) 可以把事件發送到 [*EventsManager*](#) (如果它激活的話)。事件將被當作”application”類型被消費掉。目前已支持的事件如下: 事件名稱消費于boot當應用處理它首個請求時被執行beforeStartModule在初始化模塊之前,僅當模塊被注冊時afterStartModule在初始化模塊之后,僅當模塊被注冊時beforeHandleRequest在執行分發環前afterHandleRequest在執行分發環后以下示例演示了如何將偵聽器綁定到組件: ``` <pre class="calibre14">``` <?php use Phalcon\Events\Manager as EventsManager; $eventsManager = new EventsManager(); $application->setEventsManager($eventsManager); $eventsManager->attach( "application", function ($event, $application) { // ... } ); ``` ``` ### 外部資源(External Resources) - [Github上的MVC示例](https://github.com/phalcon/mvc) | - [索引](# "總目錄") - [下一頁](# "路由(Routing)") | - [上一頁](# "Volt 模版引擎(Volt: Template Engine)") |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看