<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 控制器 ## 使用控制器 操作是處理請求的控制器上的方法。默認情況下,控制器上的所有公共方法都映射到操作,并且可由URL訪問。操作負責解釋請求和創建響應。通常,響應采用渲染視圖的形式,但也有其他方法可以創建響應。 例如,當您訪問這樣的URL時:`http://localhost/blog/posts/show/2015/the-post-title` 默認情況下,Phalcon將分解每個部分,如下所示: | 描述 | Slug | | --------------------- | -------------- | | **Phalcon目錄** | blog | | **Controller** | posts | | **Action** | show | | **Parameter** | 2015 | | **Parameter** | the-post-title | 在這種情況下,`PostsController`將處理此請求。將控制器放在應用程序中沒有特殊的位置,可以使用 `Phalcon\Loader`加載它們,因此您可以根據需要自由組織控制器。 控制器必須具有后綴`Controller`,同時操作后綴`Action`。控制器的樣本如下: ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function indexAction() { } public function showAction($year, $postTitle) { } } ``` 其他URI參數被定義為操作參數,因此可以使用局部變量輕松訪問它們。控制器可以選擇擴展`Phalcon\Mvc\Controller`。通過這樣做,控制器可以輕松訪問應用程序服務。 根據需要處理沒有默認值的參數。在PHP中照常設置參數的可選值: ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function indexAction() { } public function showAction($year = 2015, $postTitle = 'some default title') { } } ``` 參數的分配順序與它們在路徑中傳遞的順序相同。您可以通過以下方式從其名稱中獲取任意參數: ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function indexAction() { } public function showAction() { $year = $this->dispatcher->getParam('year'); $postTitle = $this->dispatcher->getParam('postTitle'); } } ``` ## 調度循環 調度循環將在Dispatcher中執行,直到沒有剩余的動作要執行。在前面的示例中,只執行了一個操作。現在我們將看到`forward()` 方法如何通過將執行轉發到不同的控制器/操作來在分派循環中提供更復雜的操作流。 ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function indexAction() { } public function showAction($year, $postTitle) { $this->flash->error( "You don't have permission to access this area" ); // Forward flow to another action $this->dispatcher->forward( [ 'controller' => 'users', 'action' => 'signin', ] ); } } ``` 如果用戶沒有訪問某個操作的權限,那么他們將被轉發到`UsersController`中的 `signin` 操作。 ```php <?php use Phalcon\Mvc\Controller; class UsersController extends Controller { public function indexAction() { } public function signinAction() { } } ``` 您可以在應用程序中使用`forwards`沒有限制,只要它們不會導致循環引用,此時您的應用程序將停止。如果調度循環沒有要調度的其他操作,調度程序將自動調用由`Phalcon\Mvc\View`管理的MVC的視圖層。 ## 初始化控制器 `Phalcon\Mvc\Controller` 提供了 `initialize()` 方法,該方法在控制器上執行任何操作之前首先執行。不建議使用 `__construct()` 方法。 ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public $settings; public function initialize() { $this->settings = [ 'mySetting' => 'value', ]; } public function saveAction() { if ($this->settings['mySetting'] === 'value') { // ... } } } ``` >[warning] 僅當`beforeExecuteRoute`事件成功執行時,才會調用`initialize()`方法。這樣可以避免初始化程序中的應用程序邏輯在未經授權的情況下執行。 ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function onConstruct() { // ... } } ``` >[warning] 請注意,即使控制器中不存在要執行的操作或用戶無權訪問它(根據開發人員提供的自定義控件訪問權限),也會執行`onConstruct()`方法。 ## 注入服務 如果控制器擴展`Phalcon\Mvc\Controller`,則可以輕松訪問應用程序中的服務容器。例如,如果我們注冊了這樣的服務: ```php <?php use Phalcon\Di; $di = new Di(); $di->set( 'storage', function () { return new Storage( '/some/directory' ); }, true ); ``` 然后,我們可以通過多種方式訪問??該服務: ```php <?php use Phalcon\Mvc\Controller; class FilesController extends Controller { public function saveAction() { // Injecting the service by just accessing the property with the same name $this->storage->save('/some/file'); // Accessing the service from the DI $this->di->get('storage')->save('/some/file'); // Another way to access the service using the magic getter $this->di->getStorage()->save('/some/file'); // Another way to access the service using the magic getter $this->getDi()->getStorage()->save('/some/file'); // Using the array-syntax $this->di['storage']->save('/some/file'); } } ``` 如果您將Phalcon用作全棧框架,則可以閱讀框架中默認提供的服務。 ## 請求和響應 假設框架提供了一組預先注冊的服務。我們將解釋如何與HTTP環境進行交互。`request`服務包含`Phalcon\Http\Request` 的實例,`response` 包含`Phalcon\Http\Response` ,表示將要發送回客戶端的內容。 ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function indexAction() { } public function saveAction() { // Check if request has made with POST if ($this->request->isPost()) { // Access POST data $customerName = $this->request->getPost('name'); $customerBorn = $this->request->getPost('born'); } } } ``` 響應對象通常不直接使用,但是在執行操作之前構建,有時 - 就像在`afterDispatch`事件中一樣 - 直接訪問響應會很有用: ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function indexAction() { } public function notFoundAction() { // Send a HTTP 404 response header $this->response->setStatusCode(404, 'Not Found'); } } ``` 在文檔`請求`和`響應`中了解有關HTTP環境的更多信息。 ## 會話數據 會話幫助我們維護請求之間的持久數據。您可以從任何控制器訪問`Phalcon\Session\Bag`以封裝需要持久化的數據: ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function indexAction() { $this->persistent->name = 'Michael'; } public function welcomeAction() { echo 'Welcome, ', $this->persistent->name; } } ``` ## 使用服務作為控制器 服務可以充當控制器,始終從服務容器請求控制器類。因此,使用其名稱注冊的任何其他類都可以輕松替換控制器: ```php <?php // Register a controller as a service $di->set( 'IndexController', function () { $component = new Component(); return $component; } ); // Register a namespaced controller as a service $di->set( 'Backend\Controllers\IndexController', function () { $component = new Component(); return $component; } ); ``` ## 控制器中的事件 控制器自動充當`調度`程序事件的偵聽器,使用這些事件名稱實現方法允許您在執行操作之前/之后實現掛鉤點: ```php <?php use Phalcon\Mvc\Controller; class PostsController extends Controller { public function beforeExecuteRoute($dispatcher) { // This is executed before every found action if ($dispatcher->getActionName() === 'save') { $this->flash->error( "You don't have permission to save posts" ); $this->dispatcher->forward( [ 'controller' => 'home', 'action' => 'index', ] ); return false; } } public function afterExecuteRoute($dispatcher) { // Executed after every found action } } ```
                  <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>

                              哎呀哎呀视频在线观看