CakePHP navigation bar View structure
In CakePHP, let's say the default.ctp just does the framing HTML, with <body>
containing $content_for_layout
only. This is great for most pages of mysite.com
. However, let's say the views within mysite.com/account/
need to share a navigation bar. Which would be the proper approa开发者_如何学运维ch for the views under the account area?
A) Make the nav bar its own element, and this element is included in each view:
<!-- settings.ctp, profile.ctp, myfiles.ctp ... -->
<div id="account_area">
<?php echo $this->element('account_nav'); }
<div>...</div>
</div>
What I don't really like in A) is that each .ctp duplicates the same wrapping code (however minimal). So perhaps something like B) is better:
B) Create a view account/index.ctp
and have each action set a $section
variable and do $this->render('/account/index')
, placing each screen in its own element:
<!-- /views/account/index.ctp -->
<div id="account_area">
<div id="account_nav">...</div>
<?php echo $this->element("account/$section"); }
</div>
C) Something else
Thanks, Brian
Typically the way I do this is Option A. But I put the $this->element('account_nav');
in the layout. This prevents putting the code in every view.
If the navigation requires configuration or disabling, just add logic to the layout and pass variables to the view as you would normally. Then you can configure it from any action if needed.
In the end, it's a hybrid approach. But from my experience provides the greatest flexibility.
I am very new to CakePHP so I don't know what specific benefits A and B might bring, but it seems like if you want to reuse the same nav bar code in multiple views (obviously a good idea), simply (C) use PHP's include function to include the required .html or .php file?
I suppose it depends on what exactly is going in to your nav bar: anything more complex than HTML links and my idea dies quite badly. :/
精彩评论