简体   繁体   中英

Structuring CakePHP Views

Long time reader, first time poster :)

I'm just embarking on my first Cake app so hopefully you guys can help me on my way.

First question is about extending/including views. I realise the way the layouts/view work is to prevent code having to be repeated, but I can't get my head around how to set up what I want to do without some repetition.

My page layout consists, apart from header and footer, a left nav bar which I want Controllers to add themselves to if appropriate, and a top nav bar which will be populated by appropriate pages within the current controller.

I tried creating a view block from within the controller but it didn't work, I'm a bit stumped.

Here's what I have:

My default layout includes the sidebar, currently just hardcoded, and the content:

Layout default.ctp

<!DOCTYPE html>
<html>
<head>.....</head>
<body>
...

<div id='leftnav'>
This is where I want my left nav
I want controllers to be able to add themselves
here.
</div>


<?php echo $this->fetch('content'); ?>

</body>
</html>

Then my /Customer/index view:

View index.ctp

<?php $this->extend('common'); ?>

<h1>Customers</h1>

.... do stuff with customers .....

Which extends my /Customer/common view to bring in the top nav bar, each view has to include this extend line, it would be nice not to have to if there's a different way of doing this.

At the moment, the links are just fixed but I'd like the controller to be able to create these options.

View common.ctp

<?php 
echo $this->Html->Link('index', "index")." ";
echo $this->Html->Link('find', 'find')." ";
echo $this->Html->Link('add', 'add')." ";
echo $this->Html->Link('details', 'details');

echo $this->Session->flash();

echo $this->fetch('content');

?>

Appreciate your help cheers! :D

I think you should just be able to put those links in your layout file. But you may have to re-write them as "$this->Html->link("Index",array("controller => $controller","action" => "index");" etc.

To get the current controller within the layout file you can say "$controller = $this->params['controller']".

right, after searching around I think I found a good way of doing at least one of these things.

For the top nav, where the links will be populated by the controller, I'll pass an array from the controller to view. Then, instead of having an ->extend in every view I'll create an element to turn the array into a nav bar, and ->fetch this in the layout.

This leads me onto my next question....
How much code is ok in a CakePHP Layout?

Hi you can use Cake PHP HTML Helper https://book.cakephp.org/3/en/views/helpers/html.html#creating-links

echo $this->Html->link('Users List', ['controller' => 'Users','action' => 'index']);

Output will be look like this:

<a href="/users/index">Users List</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM