3

Cakephp how to manage controllers and views?

 2 years ago
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.
neoserver,ios ssh client

Cakephp how to manage controllers and views?

advertisements

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK