On Tue, Jun 16, 2009 at 11:33 PM, phpcurious<[email protected]> wrote: > > why do you have to use slugs, why not try using their usernames the > users provided or assigned to them by the system?
A slug is merely any string that's been modified to be both useful and readable in a URL. So, the slug may very well be based upon a username, or even the user's name itself. > On Jun 17, 7:24 am, "Dave Maharaj :: WidePixels.com" > <[email protected]> wrote: >> I am using custom slugs that the user creates when they sign up. >> >> USER: >> id >> slug >> ......... >> >> When a User makes an entry / post or other actions on the site everything is >> tied to the slug in the URL and in the controller get the Auth->ID of the >> user for that slug.... as that's the foreign key ID, the slug is pretty much >> for URL routes. >> >> I am trying to avoid exposing any primary keys on the site and pretty much >> have it all covered except for the forms. Everything comes up >> action="posts/edit/5" or >> something along those lines letting a smart user know they are User.id => 5 Maybe that's just a bad example, but the 5 in that link would be the Post.id, not User.id. No? >> How can I go about using he slug rather than the ID? At this point I do not >> want to have to mess with tables and relationships based on foreign keys. >> >> the site has the user slug before the controller name so everything looks >> like : >> joesmith/posts/ >> joesith/profile >> so on... The way I've been dealing with this is to use slugs for Users, but IDs for admin actions. And, even for Users, I sometimes don't bother passing either a slug nor an ID and just get that from the session. The User has to be logged in, after all. Take UsersController::profile() for example: public function profile() { $this->set('profile', $this->User->getProfile($this->Auth->user('id'))); } The route makes a nice simple URL: Router::connect( '/profile', array('controller' => 'users', 'action' => 'profile') ); The profile view provides a link to edit but, again, there's no ID anywhere and I just get it from Auth. After all, I would have to check any submitted ID with what was stored in Auth's session data anyway in order to keep people from editing other Users' records. For some actions, you'll want to allow people to view records for some other User. So, you can do something like: Router::connect( '/posts/:user_slug', array('controller' => 'posts', 'action' => 'index'), array( 'pass' => array('user_slug'), 'user_slug' => '[-_a-z]+' ) ); In index(), just check if $user_slug is not null. If it isn't you can grab the ID based on it. If it is null, you just paginate as normally. However, you'd have to set routes for all of your other actions before that one. But that's not too big a deal--how many actions can you have? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
