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

Reply via email to