Say I want to call an app from the homepage

I want to display a form that I made up using the cake bake console command in my homepage. How do I go about that? I remember I did that in CakePHP 2.x by using elements, but it seemed a little not DRY. It’s a little form with a name, country, email, and comments textboxes that the user has to fill up.

In total, I am going to display two apps in the homepage. Each of them with records in the model.

I am using CakePHP 3.2.8 BTW.

What exactly do you mean by “app”? It’s a little unclear.

In general, using elements is pretty DRY, since you can also add parameters whenever you include an element:

$this->element('contact', ['target' => 'guests']);

Within the element, you can now use the $target variable; This is a fairly flexible way to reuse views.

By app I mean one which has a model, view and a controller.

I have a CRUD application, I want to display the add controller where you fill out the form, in the homepage. Basically I want to display a form in the homepage. I’ve been reading about cells, and I think it’s the way to go, but I wont show anything.

Okay, so you basically meant “cell” when talking about “app” :slight_smile:

I haven’t had any reason to use cells so far, but yes, if the form is supposed to be populated with data, or some other kind of logic is required, cells should be right tool for the job (but if the form is always empty at first, an element would be completely sufficient).

You should probably post the code you have so far, since it’s hard to know what’s wrong otherwise.

1 Like

Yes, the form is always empty at first. Think of the the blogtutorial, the add part, I want that form pasted in the homepage.

In that case an element should be the much simpler solution.

A cell makes sense if you need to create a view that consists of data from models/controllers.

So for example, if you have a block with information about a movie, it needs to get data from a movies table, an actors table, a ratings table, etc. which is then processed and sent to the view.

Without a cell, creating such a block for multiple different methods would be very un-DRY, since you’d have to fetch and process the data in every single method. With a cell, you only need to do it in one place.

However, this isn’t necessary for a simple form, since you can always use the same action no matter where you show your form. Just explicitly define the action that handles the form and it should work no matter where you include the form element.

Ok, but from the element, how would I call the form view so that it shows in the homepage.

In the past, I pasted the same thing in the “add view” to the element with some modifications.

You don’t call the form view from the element; It’s the other way around:

You simply create an element .ctp file somewhere in the elements folder and then within your add, edit, etc. view files you include it as:

<?= $this->element('my_element') ?>

Wherever you want it to be displayed within the view files.

The element file should contain the whole form.

The principle is pretty much identical as with a regular old include() php function.

It’s pretty simple and you can read the documentation on elements to find out more.