cakephp如何拥有其他控制器扩展的控制器类
我想要一个扩展 AppController 的 ThingsController。我的个人控制器将扩展 ThingsController。每个模型的功能都是重复的,并且每个模型都有自己的主要冗余控制器。
A)这是个好主意吗?
B) 我该怎么做?我尝试将其添加到controllers目录中,但cake没有找到它。
c) 我应该如何在 beforeFilter 和 beforeRender 中编码?这包括 Auth。
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 controllers directory, but cake did not find it.
c) How should I code in beforeFilter and beforeRender? That includes Auth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它会工作得很好。控制器只不过是 php 类,你可以让它们以任何你喜欢的方式继承,只要 Cake 能找到它们。
App::import('Controller', 'Things');
。class TestController extends ThingsController {}
过滤器将像平常一样继承。
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.
App::import('Controller', 'Things');
above the class definition.class TestController extends ThingsController {}
Filters will inherit like normal.
现在,您可以使用 App::uses('ClassName', 'Folder/Subfolder')
扩展类对于数据库中的表没有任何帮助...一旦您扩展了模型,您的扩展模型名称是 cake 将在数据库中查找的表名称。您不能将公共字段存储在公共表中,也不能将扩展字段存储在扩展类表中。为此,无论如何您都需要模型关联,因此在 cakePHP 中扩展模型和控制器没有太多意义。要让多个模型处理同一个表,您可以使用 $useTable 覆盖模型定义中模型使用的表,但我无法想象除了您的项目需要与您可以的表进行对话之外还有什么意义'重命名。
所以在你的情况下,我会说
汽车扩展了AppModel
,汽车扩展了AppModel
,卡车扩展了AppModel
,(普通蛋糕模型)Truck $属于汽车
,汽车$belongsTo Automobile
。将常用属性和方法放入 Automobile 中,就像您要从 Automobile 进行扩展一样,您不是继承这些方法,而是通过模型关联来访问它们,如$this->Truck->Automobile-> ;vin
使用对象表示法,而不是使用$this->Truck->vin
,这是您想要对继承执行的操作。换句话说,通过扩展 cakePHP 中的模型,您不会更接近数据库规范化——这是通过模型关联完成的。您从 AppModel 和 AppController 继承,以获得 find()、save() 等基本方法以及 beforeFilter() 和 afterRender() 等回调。当您在扩展类时,您必须在方法内调用
parent::beforeFilter()
,否则事情将会中断。我想你可以有一个表,其中包含扩展属性的所有字段(表 cars 包含字段year、vin以及box_length、trunk_litres),然后从基类扩展模型并覆盖扩展模型使用的表基类表名称(
class Car extends Auto {$useTable = auto}
),但这会在表中留下大量空字段,并且不是正确的规范化表结构。这可以说,VIN 是所有扩展类中的一个独特字段,但无需付出太多努力。但不确定 ID 的 auto_increment 在这种情况下如何工作。但随后需要额外的工作来从与扩展类'类型匹配的给定类型的公共表中提取记录(table auto has field auto_type
,class Truck extends Auto {$autoType = '卡车'}
),所以没有增益。同样,观点也没有任何收获。如果您有
class AutoController extends AppController { function displayListing()}
,然后class TruckController extends AutoController
您可以调用TruckController->display_listing()
但除非您将操作告诉$this->render('Auto/display_listing')
,否则您将需要在/View/Truck/display_listing.ctp< 中创建视图的副本/code>,如果您进行覆盖,则 <code>View/Auto/display_listing.ctp
中的视图将必须有许多 if 语句来渲染特定于 Truck 或 Car 的视图部分,因此再次,没有收获。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 thenclass TruckController extends AutoController
you can callTruckController->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 inView/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.