0

Using Codeigniter + Bonfire framework I can pass variables in my URL. This all works great with GET function like so:

in my view file I get ID of my user and on click pass it to controller again:

<a href="/my_team/index/<?php echo $user->member->id; ?>">Something</a>

controller then receives ID:

public function index($id = null) {

    //do stuff with that id...
}

URL looks like this:

http://mysite/my_team/index/26

And everything on that page knows user has ID of 26 and display information correctly.

Problem is if I manually remove that number from the URL so it looks like this:

http://mysite/my_team/index/

and leave the index in the URL I now get a whole bunch of errors because the site can't get the ID from URL. How can I avoid this? Maybe hide the index public function from the URL using .htacces or something?

LazyPeon
  • 339
  • 1
  • 19
  • Simply check whether or not you got an ID value passed in your script … and if not, act accordingly. – CBroe Aug 16 '14 at 14:20

3 Answers3

1

Like @CBroe said in his comment. Something like this:

public function index($id = null) {

   if(is_null($id)) {
      // redirect to custom error page
   }

    //do stuff with that id...
}
Pagerange
  • 234
  • 1
  • 5
0

set your CI url router to redirect

http://mysite/my_team/index/

to a default url like

http://mysite/my_team/index/1

jayxhj
  • 2,829
  • 1
  • 19
  • 24
0

The parameter could be a string value so I prefer this way

public function index($id = '') {

   if($id!='') {
      // Proceed further
   }
else{
    //Redirect to a error page(that is 301 safe)
   }
}
k10gaurav
  • 462
  • 7
  • 24