开发者

cakephp how to have a controller class that other controllers extend

开发者 https://www.devze.com 2023-03-01 01:54 出处:网络
I want a ThingsController that extends AppController.My individual controllers will extend ThingsController.The functions are repetitive for each model, and each model has its own mainly redundant con

I want a ThingsController that extends AppController. My individual controllers will extend ThingsController. The functions are repetitive for each model, and each model has its own mainly redundant controller.

A) Is this a good idea?

B) How do I do it? I tried adding it to the controller开发者_Go百科s directory, but cake did not find it.

c) How should I code in beforeFilter and beforeRender? That includes Auth.


It will work fine. Controllers are nothing more than php classes, you can have them inherit any way you like, so long as Cake can find them.

  1. Create your ThingsController and place it in app/controllers/things_controller.php
  2. In your derived controller, add App::import('Controller', 'Things'); above the class definition.
  3. Define the class properly: class TestController extends ThingsController {}

Filters will inherit like normal.


nowadays you can use App::uses('ClassName', 'Folder/Subfolder')

extending a class does nothing for you in terms of tables in the database... as soon as you extend a model, your extended model name is the table name that cake will look for in the database. you don't get to store the common fields in a common table, and the extended fields in the extended class' table. for that you need model associations anyway, so there's not much point extending models and controllers in cakePHP. to have multiple models deal with the same table, you'd override the table the model uses in the Model definition with $useTable, but I can't imagine much point in that other than your project needs to talk to tables that you can't rename.

so in your case I would say Automobile extends AppModel, Car extends AppModel, Truck extends AppModel, (normal cake models) Truck $belongsTo Automobile, Car $belongsTo Automobile. put your common properties and methods in Automobile just like as if you were going to extend from Automobile, and instead of inheriting the methods, you access them by model association as in $this->Truck->Automobile->vin with object notation rather than with $this->Truck->vin which is what you want to do with inheritance.

in other words you won't get any closer to database normalization by extending Models in cakePHP -- that is done through model associations. you inherit from AppModel and AppController in order to get the basic methods like find(), save() etc and callbacks like beforeFilter() and afterRender(), etc. WRT your question, when you override the callbacks like beforeFilter() in an extending class, you have to call parent::beforeFilter() inside the method or things will break.

i suppose you could have a table with all the fields for the extended properties in it (table automobile with fields year, vin and also box_length, trunk_litres), then extend Models from the base class and override the table which the extended models use to use the base class table name (class Car extends Auto {$useTable = auto}), but this leaves lots of empty fields in the table and is not a proper normalized table structure. it would be a way to have say, VIN be a unique field among all the extended classes without much effort though. not sure how the auto_increment for the ID works in that case though. but then it takes extra work to extract from that common table records of a given type that match the extended class' type (table auto has field auto_type, class Truck extends Auto {$autoType = 'truck'}), so no gain.

similarly there is no gain with views. if you have class AutoController extends AppController { function displayListing()} and then class TruckController extends AutoController you can call TruckController->display_listing() but unless you tell the action to $this->render('Auto/display_listing') you will need to create a duplicate of the view in /View/Truck/display_listing.ctp, and if you do the override, then the view in View/Auto/display_listing.ctp will have to have many if statements to render the portions of the view specific to Truck or Car, so again, no gain.

0

精彩评论

暂无评论...
验证码 换一张
取 消