Using Plugin Data in Theme Plugin


#1

Hello,
i’m fairly new to Cake, and i’m facing the following logical problem for me:

I’ve created a Theme as a Plugin for my Cake Site, in which i created a layout for the home page (home.ctp) and a layout for the default pages (default.ctp). Now i’ve further created a simple appointments plugin. My Question: How do i make the data from the appointment plugin available in theme plugin?

Thank you and Best Regards


#2

You pass it from the controllers to the view.
eg. in your controller you have this:

class MyController extends AppController{
  public function index(){
    $this->set('cake','the cake is not a lie!'); // name first, then the value
  }
}

then in your view (eg. plugins/CreamTheme/src/Template/MyController/index.ctp) you just display it using:

<?= h($cake); ?>

this will output the cake is not a lie! in the view!.


#3

Thank you for your answer!

In my case, i need to use the appointment object from the apointment plugin in my theme plugin. Which means that i have no access to the appointments from the AppController inside the theme plugin. Is there some way to pass the appointments to my theme plugin?


#4

You could try using a helper class?
Not sure if they are meant for that though…


#5

Yeah, i had that idea aswell. I’m just stuck in how to do so. I just simply want to load all appointments inside one of my theme-views, and display them. I tried to create a helper in my plugin, named AppointmentHelper. In the docs i read that you can use a helper inside a plugin (the theme in my case) with

public $helpers = [‘Appointment.Appointment’];

But that is not getting me anywhere. I’d wish theming would be better documentend in Cake v3, it is a bit hard to see through as a beginner


#6

public $helpers is not used anymore in CakePHP 3.x.
Instead, read this: https://book.cakephp.org/3.0/en/views/helpers.html


#7

First of all, i’m using Cake 3.5.17, which comes with croogo, which i use.

All i want is to be able to use all my appointment data in the default view of my theme. My theme is a plugin called DefaultTheme. I am not even sure if a helper is the right thing to do this way, but as you suggested it too, i want to try it that way.

I’ve now created an AppView for my theme:

/plugins/DefaultTheme/src/View/AppView.php

In this app i try to load the AppointmentHelper like this:

class AppView extends View

{

public function initialize()
{
    parent::initialize();
    $this->loadHelper('Appointment');
}

}

This doesn’t make it available inside my view. Any idea?


#8

I stumbled accross the ORM TableRegistry, and i’ve managed to find a solution for my problem:

I can access all my required data from the plugins table with the TableRegistry. In my Helper, i simply do:

$table = TableRegistry::getTableLocator()->get('Appointments');

    $appointments = $table->find()
        ->where([
            'status' => 1
        ])
        ->all();

and let the function return $appointments. Now i can use the data in my default view for my theme. It feels wrong though, if someone can help me to find a better way i’d approve.


#9

What you are looking for is View Cells. Accessing the model layer from a helper is considered bad practise and breaks the MVC architecture.