CodeIgniter Forums
Can you suggest a better URI vs CRUD system for a forum? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can you suggest a better URI vs CRUD system for a forum? (/thread-52669.html)



Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-20-2012

[eluser]nullsys[/eluser]
Hi!
I'm hoping you can help.
I'm considering the best way to present a forums' URL, vs Controller setup.
Here is what I'm thinking:

Code:
// Gets all categories
site.com/forum/

// Gets topics in the "dog" category
site.com/forum/dogs

// Gets all posts in the "huskie" topic
site.com/forum/dogs/huskie


//Create, edit and delete examples
site.com/forum/create/category
site.com/forum/create/topic
site.com/forum/create/post

site.com/forum/edit/category/cat_id
site.com/forum/edit/topic/topic_id
site.com/forum/edit/post/post_id

site.com/forum/delete/category/cat_id
site.com/forum/delete/topic/topic_id
site.com/forum/delete/post/post_id

// View single post
site.com/forum/view/post_id

Only the post_id will be numeric.

My problem is this:
Should I follow the standard CRUD format in the controller, and do the following for each create, retrieve, update and delete:

Code:
public function create() {
if ($this->uri->segment(3, 0) === "category") {
//Create category
}
if ($this->uri->segment(3, 0) === "topic") {
//Create topic
}
if ($this->uri->segment(3, 0) === "post") {
//Create post
}
}

I feel this would work, but I can't help feeling there is a more "codeigniter" way, using Routes or some such.

Any feedback, suggestions or the likes would be greatly appreciated!
Thank you.


Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-20-2012

[eluser]CodeIgniteMe[/eluser]
a separate function for each functionality is more object-oriented way of getting things done as to simplify the code contents.
you can use the routes to re-route the execution to your CRUD controller:

This example route will trigger once he encounters each of your given example routes
Code:
$route['(create|edit|delete)/(category|topic|post)/(:any)'] = 'CRUD/$1/$2/$3';
$route['(create|edit|delete)/(category|topic|post)'] = 'CRUD/$1/$2';

then at the controller, you can try it by having something like this

Code:
<?php
class CRUD extends CI_Controller{
function create($param1,$param2='')
{
  echo $param1.'<br/>'.$param2;
}
function edit($param1,$param2='')
{
  echo $param1.'<br/>'.$param2;
}
function delete($param1,$param2='')
{
  echo $param1.'<br/>'.$param2;
}
}



Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-21-2012

[eluser]nullsys[/eluser]
Hi!
Thanks for the tips on route and CRUD.
But I'm still a little confused.
I like the $param system, but I'm still going to have to use the following:

Code:
if ($param1 === "category") {
//Create category
}

Inside each of the create/edit/delete methods right?
otherwise, how else would the controller know which one I want to edit, etc.


Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-21-2012

[eluser]PhilTem[/eluser]
[quote author="nullsys" date="1340286836"]
I like the $param system, but I'm still going to have to use the following:

Code:
if ($param1 === "category") {
//Create category
}

Inside each of the create/edit/delete methods right?
otherwise, how else would the controller know which one I want to edit, etc.[/quote]

You're right. However, I think you might also use

Code:
$route['(create|edit|delete)/(category|topic|post)/(:any)'] = 'CRUD/$2_$1/$3';
$route['(create|edit|delete)/(category|topic|post)'] = 'CRUD/$2_$1';

Watch the added underscore between $2 and $1 as well ass the switched parameters $1 and $2.

That way (I'm not 100% sure this will work since my RegExp knowledge is very low) you can have methods like

Code:
function category_edit($param_3) {}
function category_create($param_3) {}
function category_delete($param_3) {}

And so on a so forth for topic and post the same.
This would save you time writing the

Code:
if ($param1 === "category") {
//Create category
}

parts


Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-21-2012

[eluser]nullsys[/eluser]
HI Philterm,
Thanks for that, I've been playing around, modified it further and come up with this:

Code:
public function create($param1='',$param2='')
{
  if ($param1 === "category" OR $param1 === "topic" OR $param1 === "post")
  {
    $this->load->helper('form');
    $this->load->library('form_validation');
    if ($this->form_validation->run('create_'.$param1) === FALSE)
    {
     $this->load->view('forum/create_'.$param1);
    }
    else
    {
     //Work with the model and create entries
    }
  }
  else
  {
   //URL failure, no matches
  }
}

The reasoning behind it is trying to keep DRY code. Rather can repeat functions for each "category", "topic" etc. I can just use create(), edit(), etc.

Can you seen any problems with this?


Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-21-2012

[eluser]CodeIgniteMe[/eluser]
Hi @PhilTem
If you want to use this route
Code:
$route['(create|edit|delete)/(category|topic|post)'] = 'CRUD/$2_$1';
you should modify the following methods
Code:
function category_edit($param_3) {}
function category_create($param_3) {}
function category_delete($param_3) {}
to accept optional parameters if your URI has no 3rd param/$param_3

@nulsys
I like how you code, setting a reusable set of validation rules is the CodeIgniter way. However, you may not need to load the form helper because the form_validation library needs it and will load it automatically.


Can you suggest a better URI vs CRUD system for a forum? - El Forum - 06-23-2012

[eluser]nullsys[/eluser]
Thanks everyone.
I appreciate that comment CodeIgniteMe too lol.

I'm still working on it, but I learnt something here, so thanks all!