CakePHP common models
I have 3-4 apps running of the same Cake library, each app has its own controllers, models and views.
I have found recently that quite a few methods within the models overlap between projects, i was wondering if it was possible to get the app models (not app_model.php) to extend the model开发者_运维百科s in the cake files
e.g.
app1
controllers
models
model_1.php
model_2.php
views
app2
controllers
models
model_1.php
model_3.php
views
cake
controllers
models
model_1.php
views
I hope that makes sense
You shouldn't really be modifying your CakePHP applications to read code from another CakePHP application. All of your apps code should be encapsulated within that application.
If you find yourself having the same code across multiple applications, this sounds like a perfect candidate for extracting the code out into a CakePHP plugin!
Check out the CakePHP documentation for creating plugins.
A plugin will have it's own controllers, models, view, and now (as of CakePHP 1.3) it's own assets (CSS, Js, Images, etc). You can add the plugin to your application and modify your routing and links so that the end user notices no difference in your application.
Router::connect('/your/routes/*', array('plugin' => 'your_plugin', 'controller' => 'controller_in_plugin'));
echo $this->Html->link('Your Link', array('plugin' => 'your_plugin', 'controller' => 'controller_in_plugin', 'action' => 'action_in_plugins_controller'));
If you're using CakePHP 1.3, then you can modify your bootstrap.php file:
App::build(array(
...
'models' => array('/var/www/app1/models/', '/var/www/cake/models/'),
...
));
But I think you'd have to rename them differently, i.e. you can't have model_1.php in both places. But you could have vehicle.php
in your cake/models folder, car.php
in your cars_app/models folder & bike.php
in your bikes_app/models folder with the models extending the Vehicle model.
I haven't tested it, but give it a shot!
You could try using symbolic link for the common models. This way even though there are multiple files in the directories, they all share the same root implementation.
As mentioned by RabidFire, you first have to specify an additional model path. Then, to inherit from such a model, you have to import the respective model in the file with the child model:
App::import('Model', 'ParentModel');
class ChildModel extends ParentModel {
...
}
精彩评论