0
votes

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

2

2 Answers

2
votes

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.

0
votes

Each View has to have it's own method in the Controller.

Say, if you want an ideas/index, ideas/view/x and ideas/add pages, you need to create the following methods:

class IdeasController extends AppController
{

    public function index()
    {
        /* index stuff */
    }

    public function view($id)
    {
        /* view stuff */
    }

    public function add()
    {
        /* add stuff */
    }
}

For the sake of understanding the framework, is advisible to do the blog tutorial, as you after that will be very familiar with the core concepts: http://book.cakephp.org/2.0/en/getting-started.html#blog-tutorial

Good luck!