Forum Moderators: coopster

Message Too Old, No Replies

CakePHP, best practices for structuring application

CakePHP, best practices for structuring application

         

Dabrowski

8:54 pm on Sep 25, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys!

Was on this forum years ago, not been for a while but I remember people being very knowledgeable and helpful, I'm hoping they still are :)

I'm having a go at writing an app using CakePHP and Bootstrap, loving it so far! It's all going very well but, this being my first app, I want to ask opinions on structure before I do it wrong and have to redo it all later.

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 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 functions 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="title-header">
.....
</div>

<div class='container-fluid b-lg'>
<div class='row'>
<div id='leftnav' class='col-xs-2'>
This is where I want my left nav
I want controllers to be able to add themselves
here.
</div>
<div class="col-xs-10 b-w">

<?php echo $this->Session->flash(); ?>

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

<br><br>
</div>
</div>
</div>

</body>
</html>


Then my /Customer/index view:

View index.ctp

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

<h1>Customers</h1>

<?php

foreach ($customers as $customer) {
echo "<b>";
echo $this->Html->link( $customer['Customer']['business_name'], "details/".$customer['Customer']['id']);

echo "</b><br>";
}

?>


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->fetch('content');

?>



Hmmm that was quite a lot of stuff, I think I'll leave it at that one question for now!

Appreciate your help cheers! :D

Dabrowski

7:57 pm on Sep 27, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nobody huh?