1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Phalcon forwardしたときにAssetsを切り替える

Last updated at Posted at 2015-06-14

エラーページにforwardするときにCSSやJSのファイルを切り替えたいなー、と思って調べてみた。

##環境
PhalconPHP 1.3.5

##やり方
以下の投稿に載ってたので、その方法を拝借した。

Is there some way to add an asset to every single page in Phalcon - Stack Overflow
http://stackoverflow.com/questions/25841310/is-there-some-way-to-add-an-asset-to-every-single-page-in-phalcon

IndexController.php
<?php

class IndexController extends \Phalcon\Mvc\Controller
{
    public function afterExecuteRoute()
    {
        // このままディスパッチループが終了する場合にのみAssetsを設定する
        if (!$this->dispatcher->isFinished()) {
            return;
        }
        $this->assets->addCss('css/index.css');
        $this->assets->addJs('js/index.js');
    }

    public function indexAction()
    {
        // ErrorControllerにforwardすると仮定
        $this->dispatcher->forward([
            'controller' => 'error',
            'action'     => 'error404'
        ]);
    }
}
ErrorController.php
<?php

class IndexController extends \Phalcon\Mvc\Controller
{
    public function afterExecuteRoute()
    {
        $this->assets->addCss('css/error.css');
        $this->assets->addJs('js/error.js');
    }

    public function error404Action()
    {
         // エラー時(404)の処理を記述
    }
}

"切り替える"と表現したが、forwardする場合にはAssetsを設定しないようにしているだけ。
ちょっと強引なやり方かもしれないので、もっといい方法があれば教えて頂けるとありがたいです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?