Re: Best practices question

2010-01-08 Thread Dr. Loboto
Just a little additions:

// always have default value for action args to not receive
missing argument errors
function view($username = null) {
if (!$username) {
$this-Session-setFlash(__('Invalid User.',
true));
$this-redirect('/pages/error');
// exit is not needed, redirect do it itself
}
if ($user = $this-User-findByUsername($username)) {
$this-set('user', $user);
}
   else {   // always check (done) and react (missed) to
everything
   $this-Session-setFlash(__('Invalid User.',
true));
$this-redirect('/pages/error');
}
if ($this-Auth-user('role') == 'admin') {
$this-Session-setFlash(__('You have admin
access', true));
}
}

On Jan 7, 9:20 pm, Foroct forrestfraz...@gmail.com wrote:
 I have a users table and would like to view my users by username with
 a url like example.com/users/view/username
 In order to do that I have set my view function in my users controller
 to
 function view($username) {
                 if (!$username) {
                         $this-Session-setFlash(__('Invalid User.', true));
                         $this-redirect('/pages/error');
                         exit();
                 }
                 if ($user = $this-User-findByUsername($username)) {
                         $this-set('user', $user);
                 }
                 if ($this-Auth-user('role') == 'admin') {
                         // $this-flash('You have admin access');
                         $this-Session-setFlash(__('You have admin access', 
 true));
                 }
         }

 While this will work correctly, showing an existing users profile it
 fails to redirect to an error page when you replace the username with
 one that doesn't exist. As I was looking for an answer to this I found
 a few articles that state I should always set my function view to $id
 then rewrite the url to contain the username.

 I was wondering if there was a best practice to follow or if what I am
 doing is okay?
Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: Best practices question

2010-01-08 Thread Foroct
Thank you that works like a charm.  Usually when I write straight PHP
I include an else statement but for some reason when doing this (my
first cakePHP project) I did not do that.



On Jan 8, 4:36 am, Dr. Loboto drlob...@gmail.com wrote:
 Just a little additions:

         // always have default value for action args to not receive
 missing argument errors
         function view($username = null) {
                 if (!$username) {
                         $this-Session-setFlash(__('Invalid User.',
 true));
                         $this-redirect('/pages/error');
                         // exit is not needed, redirect do it itself
                 }
                 if ($user = $this-User-findByUsername($username)) {
                         $this-set('user', $user);
                 }
                else {   // always check (done) and react (missed) to
 everything
                        $this-Session-setFlash(__('Invalid User.',
 true));
                         $this-redirect('/pages/error');
                 }
                 if ($this-Auth-user('role') == 'admin') {
                         $this-Session-setFlash(__('You have admin
 access', true));
                 }
         }

 On Jan 7, 9:20 pm, Foroct forrestfraz...@gmail.com wrote:

  I have a users table and would like to view my users by username with
  a url like example.com/users/view/username
  In order to do that I have set my view function in my users controller
  to
  function view($username) {
                  if (!$username) {
                          $this-Session-setFlash(__('Invalid User.', true));
                          $this-redirect('/pages/error');
                          exit();
                  }
                  if ($user = $this-User-findByUsername($username)) {
                          $this-set('user', $user);
                  }
                  if ($this-Auth-user('role') == 'admin') {
                          // $this-flash('You have admin access');
                          $this-Session-setFlash(__('You have admin 
  access', true));
                  }
          }

  While this will work correctly, showing an existing users profile it
  fails to redirect to an error page when you replace the username with
  one that doesn't exist. As I was looking for an answer to this I found
  a few articles that state I should always set my function view to $id
  then rewrite the url to contain the username.

  I was wondering if there was a best practice to follow or if what I am
  doing is okay?
Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: Best Practices Question

2009-03-03 Thread brian

I'd put as much as possible in its appropriate controller. And as much
of the logic in the appropriate model.

On Tue, Mar 3, 2009 at 4:47 PM, Dave Maharaj :: WidePixels.com
d...@widepixels.com wrote:
 I have a controller USERS which displays a users profile. The profile data
 is comprised of information from various HABTM and hasMany tables in the
 database  (PROFILE, AUTHORS, ARTISTS, QUOTES and so on)
 The user can edit all of their preferences / info on the profile page using
 AJAX. All of the AJAX forms point to a function in the users_controller. Now
 I currently have all the actions such as update_authors, delete_quote,
 new_quote, update_artists in the USER Controller.

 My question is should I have put :
  delete_quote, new_quote in the quotes_controller
     update_authors in the authors_controller
     update_artists  in the artists_controller

 Everything is working fine but seems really jammed having 30+ functions in
 the one controller.

 So is it best to put them into their related controller because that's how
 it should be set up? Leave it the way it is because it working? what is the
 right way or best way?

 thanks,

 Dave

 


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Best Practices Question

2009-03-03 Thread keymaster

I'd define the following basic crud operations operations in
appController to work generically independant of controller/model:

add(), edit(), del(), view()
admin_add(), admin_edit(), admin_del(), admin_view()

Then, all your profile controllers would inherit from appController,
and:

delete_quote, new_quote functions would become /quotes/del, /quotes/
add
update_authors would become authors/edit
update_artists would become artists/edit

... but you wouldn't need to build these crud operations separately
into each controller.
You would still need different view files for each controller
obviously.

Often times I find there are controllers which are only there for
their crud operations. In that case, I would want to take things a
step further and create a generic crud controller, which works with
any model and performs crud operations on it, then eliminate the other
crud-only controllers.

I haven't done this yet, because it would mean storing all the view
files in a views/cruds/ folder, named edit_authors, edit_quotes and
the like. Then would have to do a $this-render('edit_'.$controller);
in the crud controller.

This is somewhat of a departure from the way things are done in cake,
so I am still mulling it over.

But since I have generic crud functions in my app controller, it
drives me crazy having all these empty controllers hanging around just
for inheriting the crud ops.

... now if only cake was smart enough to create a controller if we
don't supply one (the way I understand it does with models), that
would solve the problem.

How do other people deal with the issue of controllers whose only
raison d'etre is their crud operations?

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Best Practices Question

2009-03-03 Thread keymaster

Have a look at this:
http://groups.google.com/group/cake-php/browse_thread/thread/7638b690ae0545ff/53a804265793ace3?lnk=gstq=crud#

The more I think about it, the more sold I am on grigri's approach in
the above thread.

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---