>
> Hi,
>
> I seem to be struggeling with possibly something so obvious, i can't
> find it in the tutorials or books. Hopefully someone here is able to
> enlight me. I have made a good number of sites with PHP and just moved
> to cakephp as a mvc framework. I completed the blog and auth/acl
> tutorials. So far I like the speed of development, however I don't
> seem to grasp how to properly create a page combining multiple views,
> leaving the mvc framework (with auth in tact).
>
> E.g. In the blog tutorial a simple blog is created based on a model
> for post. This model has functions for view and add, in the blog
> tutorial you either view posts or add a posts.
>
> I would like to create a page that shows the last top 10 posts, AND an
> add form to add posts when needed, as effectively as possible.
>
> The way I ended up doing it is to change the index to display a form
> for the add post, linking to add, and a seperate table viewing the
> lasts posts. However I basically copied the code for the 'view.ctp'
> and add.ctp', which means dublicate code. For this reason this does
> not seem to be the proper way. Not only because I have dublicate code,
> also because the add form is now visible to everyone while 'guests'
> can't posts. (based on the ACL, guests should not be able to add
> posts, only view them)
>
> I would like to understand what best practice is to cover this in
> cakePHP;
> -> Create a page seperate from the models;
> -> Create elements, in this case to show and add, that calls the
> functions controller?
> -> Use requestAction to call controller functions from within those
> elements?
>
> If someone whould have (a link to) an example that would be highly
> appreciated.
>
> Thanks in advanced for your response,
>
> Reinder
>
> >
>
What I usually do for my forms is to put them in an element. So, for
Post model, I'd have in views/posts/add.ctp a line with
$this->element('posts/form'). And the file
views/elements/posts/form.ctp includes all of the logic for creating
the form, including whether to show an add or edit form, stuff for
admins only, etc.
switch ($this->params['action'])
{
case 'edit':
echo $form->create('Post', array('action' => 'edit'));
echo $form->hidden('Post.id');
echo $form->hidden('Post.user_id');
break;
case 'add':
default:
echo $form->create('Post', array('action' => 'add'));
echo $form->hidden('Post.user_id', array('value' => $user_id));
}
This way, you can include the form in your add or edit views, as well
as in your index view. Put a debug($this->params) in your view
somewhere to get an idea how that's set up.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment