CodeIgniter Forums
Routing with dynamic Event ID in URL for event managment system - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Routing with dynamic Event ID in URL for event managment system (/showthread.php?tid=54435)

Pages: 1 2


Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]Saquib[/eluser]
Hi Guys,

I am developing an event management system using PHP Codeigniter and MySQL where I am not sure how i can solve below problems

Problem 1

I want to make URL for back end like

Quote:www.event-backend.com/event/EVENt_ID (for dashboard)
www.event-backend.com/event/EVENt_ID/registration
www.event-backend.com/event/EVENt_ID/travel
and so on

and by changing the EVENT_ID i can open different event

But in codeigniter how I can do it, second param will be a method of controller, i need it dynamic. what will be the routing ?

Quote:Problem 2

I want to people can access event website (which is unique for every event) by typing

Quote:www.somedomain.com
which will be pointing at
www.event-backend.com/website/EVENt_ID

and


www.somedomain.com/registration
which will be pointing at
www.event-backend.com/website/EVENt_ID/registration


Please help



Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]PhilTem[/eluser]
From the CI user's guide: http://Remapping Function Calls


Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]Saquib[/eluser]
Hi Phil, if you can post some code it will be very helpful

I have a controller named event, which has other methods
->registration
->travel

etc.

I want to access these methods as follows

www.event-backend.com/event/EVENt_ID/registration
www.event-backend.com/event/EVENt_ID/travel

I have gone through User guide in URI Routing section, but am not sure how i can access other methods in controller


Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]TWP Marketing[/eluser]
[quote author="Saquib" date="1347120616"]Hi Phil, if you can post some code it will be very helpful

I have a controller named event, which has other methods
->registration
->travel

etc.

I want to access these methods as follows

www.event-backend.com/event/EVENt_ID/registration
www.event-backend.com/event/EVENt_ID/travel

I have gone through User guide in URI Routing section, but am not sure how i can access other methods in controller[/quote]

Using CI's URI sequence: /controller_name/method_name/param1
Code:
www.event-backend.com/event/registration/EVENt_ID
   www.event-backend.com/event/travel/EVENt_ID



Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]Saquib[/eluser]
[quote author="TWP Marketing" date="1347121909"][quote author="Saquib" date="1347120616"]
Using CI's URI sequence: /controller_name/method_name/param1
Code:
www.event-backend.com/event/registration/EVENt_ID
   www.event-backend.com/event/travel/EVENt_ID
[/quote]

I am not sure it is a good idea, because what happen when i need to paginate or pass other params.

I need to keep EVENT_ID in methods place and then methods should come which will work for every event

Code:
www.event-backend.com/event/EVENT_ID/registration



Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]TWP Marketing[/eluser]
You can pass as many parameters as you wish:
Code:
/controller/method/param1/param2/param3/...
They can be accessed using the URI class:
Code:
$param1 = $this->uri->segment(3); // note the segment numbers
$param2 = $this->uri->segment(4);
$param3 = $this->uri->segment(5);
This is explained in the User Guide:
http://ellislab.com/codeigniter/user-guide/libraries/uri.html


Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]Saquib[/eluser]
[quote author="TWP Marketing" date="1347124284"]You can pass as many parameters as you wish:
Code:
/controller/method/param1/param2/param3/...
They can be accessed using the URI class:
Code:
$param1 = $this->uri->segment(3); // note the segment numbers
$param2 = $this->uri->segment(4);
$param3 = $this->uri->segment(5);
This is explained in the User Guide:
http://ellislab.com/codeigniter/user-guide/libraries/uri.html[/quote]

I know it can be done using above pattern.
But here we are missing that i wanted to have my EVENT_ID in place of controller methods place, How can we get below URL, is it possible Sad

Code:
www.event-backend.com/event/EVENT_ID/registration

I think it is possible with some routing but I can't figure out how ?


Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]PhilTem[/eluser]
Okay, some really quick and dirty code for the lazy:

With Routing:
Code:
$route['event/(:any)/registration'] = 'event/registration/$1';
$route['event/(:any)/join'] = 'event/join/$1';
in the controller
Code:
class Event extends CI_Controller {
  function registration($event_id)
  {
    // do registration stuff here
  }
  
  function join($event_id)
  {
    // do join stuff here
  }
}

Or, with _remap, there's no need for routing!
Code:
class Event extends CI_Controller {
  function _remap()
  {
    $event_id = $this->uri->segment(2);
    
    if ( $method = $this->uri->segment(3) )
    {
      return call_user_func_array(array(&$this, $method), $event_id);
    }
    else
    {
      // Do whatever you want to do when the URI is just like
      // www.example.com/event/EVENT_ID
    }
  }
  
    function registration($event_id)
  {
    // do registration stuff here
  }
  
  function join($event_id)
  {
    // do join stuff here
  }
}



Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]TWP Marketing[/eluser]
You could probably do it with routing, but I have not tried it and prefer to use MVC standard url's.

I find your method to be reversed from normal and CI was not designed to reverse the position of the method and parameters. I think your URL makes more work for you and certainly bends the CI and MVC paradymes.


Routing with dynamic Event ID in URL for event managment system - El Forum - 09-08-2012

[eluser]Saquib[/eluser]
[quote author="PhilTem" date="1347126473"]Okay, some really quick and dirty code for the lazy:
Or, with _remap, there's no need for routing!
Code:
class Event extends CI_Controller {
  function _remap()
  {
    $event_id = $this->uri->segment(2);
    
    if ( $method = $this->uri->segment(3) )
    {
      return call_user_func_array(array(&$this, $method), $event_id);
    }
    else
    {
      // Do whatever you want to do when the URI is just like
      // www.example.com/event/EVENT_ID
    }
  }
  
    function registration($event_id)
  {
    // do registration stuff here
  }
  
  function join($event_id)
  {
    // do join stuff here
  }
}
[/quote]

Thanks Phil, I will go with that, what about second problem, someone can help. I don;t know how to point any domain to my app on a URL

Quote:I want to people can access event website (which is unique for every event) by typing

Code:
www.somedomain.com
        which will be pointing at
    www.event-backend.com/website/EVENt_ID

or

Code:
www.somedomain.com/registration
        which will be pointing at
    www.event-backend.com/website/EVENt_ID/registration