Reputation: 109
I'm trying to make an API Client with CakePHP 3.0. I'm using Http Client (http://book.cakephp.org/3.0/en/core-libraries/httpclient.html) to make all the requests but I don't need to use a database so at least for now I don't need Models. Suppose you have:
Now from CarsController I want to get (from an API) all the users (using a method into UsersController). How can I access UsersController from CarsController using CakePHP 3.0? All the docs I read are just for apps with databases (using models mapped to db). Thank you!
Upvotes: 2
Views: 3412
Reputation: 60463
You don't do that!
If you need to access another controller, and this cannot be logically solved via inheritance, then this indicates that you are doing something wrong, and that you're planning to violate the MVC principle that CakePHP proposes.
If you need to share functionality accross otherwise independent controllers, then you should look into moving that functionality into components (which kind of represent a service layer), service classes (which could be part of the model layer), or models.
Since in CakePHP 3.x the models, or let's better say the table classes of the model layer, are highly database centric, and you don't want to use a database, you should maybe choose one of the two former options, ie components or service classes, but it really depends, your model layer can of course hold any business functionality, and not even a table class is forced to communicate with a database, its datasource can be whatever you implement it to be.
Upvotes: 5
Reputation: 145
Personally, I don't know why cake does not make this more accessible. As components, does not do the job as you don't have access to all the classes etc. Therefor most revert to $this->requestAction
depending on the need, one other option is to redirect in action1 to action2 - action2 would be something like
public function action2($controller=null, $action=null){
, then do what it is supposed to do and redirect back to action 1
Upvotes: 0
Reputation: 1356
If you really need to, you can use Cake\Routing\RequestActionTrait::requestAction()
method
$this->requestAction([
'controller' => 'Users',
'action' => 'fancyAction'
]);
Read more from documentation (http://book.cakephp.org/3.0/en/development/routing.html#requestactiontrait)
Upvotes: 1