5
7

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.

[CakePHP 2.x]Model,View,Controllerで共通する処理

Last updated at Posted at 2015-02-17

Model, View, Controllerで共通する処理を下記ブログを参考にVendorに作成。

Vendorではファイル名に小文字を使うように記載があったので小文字にする。


共通処理をVendor配下に作成。
app/Vendor/util/mylib.php

namespace Util;

class Mylib {
    public function process() {
        /** 処理 */
    }
}

  • 利用したい箇所の頭に「App::import('Vendor', 'util/mylib');」を追加。
  • 利用したい箇所で利用する。

Controllerで利用。
app/Controller/SampleController.php

App::uses('AppController', 'Controller');
App::import('Vendor', 'util/mylib');

class SampleController extends AppController {
    public function index() {
        $mylib = new Util\Mylib();
        $mylib->process();
    }
}

Taskで利用。
app/Console/Command/Task/SampleTask.php

App::import('Vendor', 'util/mylib');

class SampleTask extends Shell {
    public function execute() {
        $mylib = new Util\Mylib();
        $mylib->process();
    }
}

Helperで利用。
app/View/Helper/SampleHelper.php

App::uses('AppHelper', 'View/Helper');
App::import('Vendor', 'util/mylib');

class SampleHelper extends AppHelper {
    public function process() {
        $mylib = new Util\Mylib();
        $mylib->process();
    }
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?