Cakephp how to manage controllers and views?
source link: https://www.codesd.com/item/cakephp-how-to-manage-controllers-and-views.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cakephp how to manage controllers and views?
I am completely new to CakePHP, therefore I am fairly confused on how the management of different controls/views/elements works.
The Problem: I am attempting to create my home page, which has PageController and view home.ctp. Now, I have another controller, IdeaController, which contains function to load all the ideas from the DB.
public function index() {
$this->set('title_for_layout', 'Ideas');
$this->set('ideas', $this->Idea->find('all'));
}
Now, I need to access this Idea Controller from multiple(up to 3) different views. What is the best way to go about doing so?
Thank-you
You're going the other way around...
Views don't "have access" to the controller. The controller receives a request, tells the models what info it need from them, and passes all necessary data to the views. Views are like little dolls you can put clothes on, but nothing more, they don't speak or play or ask random questions to you.
Generally speaking, there's just one view per action in a controller. If you need to "access" the controller from different views, you may be a little confused with cake.
Without knowing the exact reason you need to do this, I'm going to assume you want to "access" the controller from 3 different views only to get all ideas from the BD. If that's the case, the standard way is:
1) Create a function in the appropriate model:
//Idea Model
public function getAll() {
return $this->Idea->find('all');
}
Well, you don't actually need to do that function, since it's so simple, but for more complex cases where you want to get only the ideas of the logged in user and in a certain order, encapsulating that in the model is a mentally-sane way to go.
2) In every action in the controller that the view will need that data, you do
public function randomAction() {
$this->set('ideas', $this->Idea->getAll());
}
3) In every view you receive the data as
pr($ideas);
And that's it.
The views never "access" the controller, so your question turns out to be a bit confusing. Maybe you meant having 3 views associated to the same action, but there's little reason to do that also... Or maybe you meant to have a "view" in a lot of other "views", like the "info" bar on stackoverflow, that repeats everywhere so you need to have it in a lot of views. If that was the case, then you have to do that in an element to reutilize that "view"-code inside other views.
If you clarify a little more what you want, I can probably answer a lot better, but I hope at least I gave you some direction on where to look or how to go about what you want to do.
Recommend
-
155
Installs Daily installs, averaged monthly Sorry, the graph can't be displayed because your browser doesn't support...
-
94
Learn to use the CakePHP framework The official CakePHP online training courses provide you the opportunity to learn from the experts in the CakePHP community, from the comfort of your...
-
83
CakeFest, the official CakePHP Conference.
-
49
Latest news and articles about CakePHP
-
55
Latest news and articles about CakePHP
-
67
CakeFest, The CakePHP Conference, has a packed and fun conference and workshop schedule
-
84
Latest news and articles about CakePHP
-
7
AJAX Pagination and Sorting with CakePHP 2.x Submitted by NanoDano on Thu, 07/11/2013 - 22:09 CakePHP comes with a core JsHelp...
-
4
Intermediate Rails: Understanding Models, Views and Controllers I’m glad people liked the introduction to Rails; now you scallawags get to...
-
3
User management, authentication and plugin acl for CakePHP? advertisements I am pretty new to CakePHP, having been using rails for a little wh...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK