Welcome Guest, Not a member yet? Register   Sign In
Best Practice: Controller that handles multiple access methods (url/uri, ajax, post)
#1

[eluser]Unknown[/eluser]
I am curious how others are coding controller functions that do one thing, but can be accessed through multiple methods. For the sake of example, imagine a controller for administrating categories.

1. You want to be able to delete categories via a hyperlink that is captured by javascript and handled by an ajax request, with a JSON response.
2. In the interest of being degradable, you want the hyperlink (something like "/admin/categories/delete/27") to be functional, in case javascript is turned off.
3. Elsewhere in the admin, you have a page that allows you to delete multiple categories or some other method that involves posting the category_id.

The most obvious three techniques I can think of are included below, but they all seem a bit clunky. How are you handing these types of situations in your controller code?

(These are all coded quickly for this post and not actually used, just a general idea...)

Possible Technique 1: One Function

Code:
function delete() {
    if(intval($this->uri->segment(4))) {
        $category_id = (int) $this->uri->segment(4);
        $this->category_model->delete($category_id);
        $this->load->view("admin/categories/delete_succes");
    } else {
        $category_id = (int) $this->input->post('category_id');
        $this->category_model->delete($category_id);
        if($this->user_agent->is_ajax()) {
            $json = array(
                "result" => "success",
                "message" => "The category was successfully deleted."
            );
            $this->load->view("json", $json);
        } else {
            $this->load->view("admin/categories/delete_succes");
        }
    }
}

Possible Technique 2: Separate functions

Code:
function ajax_delete() {
    $category_id = (int) $this->input->post('category_id');
    $this->category_model->delete($category_id);
    $json = array(
        "result" => "success",
        "message" => "The category was successfully deleted."
    );
    $this->load->view("json", $json);
}

function delete() {
    $category_id = (int) $this->uri->segment(4);
    $this->category_model->delete($category_id);
    $this->load->view("admin/categories/delete_succes");
}

function post_delete() {
    $category_id = (int) $this->input->post('category_id');
    $this->category_model->delete($category_id);
    $this->load->view("admin/categories/delete_succes");
}

Possible Technique 3: Utility Functions

Code:
function delete() {
    if(intval($this->uri->segment(4))) {
        $this->_process_delete($this->uri->segment(4));
    } else {
        $category_id = (int) $this->input->post('category_id');
        if($this->user_agent->is_ajax()) {
            $this->_process_ajax_delete($category_id);
        } else {
            $this->_process_delete($category_id);
        }
    }
}

Thanks in advance.
#2

[eluser]GSV Sleeper Service[/eluser]
IMO it's a bad idea to use 'GET' for deletion, imagine if somehow your secure area became unsecure, googlebot would quite happily spider your admin area deleting all your content as it goes!
#3

[eluser]Johan André[/eluser]
I would go for the first approach. It's very slick!

I usually define a constant in application/config/constants.php:

Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

Then in my controllers (or views):

Code:
if(IS_AJAX)
{
   // Do the ajax magic thing
} else {
   // Do the standard stuff (non-ajax)
}

I guess you extended the useragent-lib since there is no is_ajax()-method built-in?
#4

[eluser]jcavard[/eluser]
Thank Johan André! It's so simple yet brilliant! neat thanks!

[quote author="Johan André" date="1252421870"]I would go for the first approach. It's very slick!

I usually define a constant in application/config/constants.php:

Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

Then in my controllers (or views):

Code:
if(IS_AJAX)
{
   // Do the ajax magic thing
} else {
   // Do the standard stuff (non-ajax)
}

I guess you extended the useragent-lib since there is no is_ajax()-method built-in?[/quote]
#5

[eluser]adityajoshi[/eluser]
i am having similar problem

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome to my blog</h1>
<ol>
<div = "posts">
&lt;?php foreach($query->result() as $row): ?&gt;
&lt;?php echo $row->title ?&gt;
&lt;?php echo $row->text ?&gt;
</div>
&lt;?php
$comment_display = $this->comment_display($row->id);
?&gt;
&lt;form method = "post" action = "http://localhost/feeders/index.php/blog/comment_insert"&gt;
&lt;?php echo form_hidden('entry_id',$this->uri->segment(3)); ?&gt;
<p>&lt;textarea name="text" rows="3"&gt;&lt;/textarea></p>
<p>&lt;input type="text" name="author" /&gt;&lt;/textarea></p>
<p>&lt;input type="submit" value="Submit" /&gt;&lt;/textarea></p>
&lt;/form&gt;
<hr />
&lt;?php endforeach; ?&gt;

</ol>
&lt;/body&gt;
&lt;/html&gt;



My controller is
&lt;?php
var_dump($_POST);
class Blog extends Controller {
function Blog()
{
parent::Controller();
$this->load->scaffolding('comments');
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('blog_model');

}

function index()
{

$data['title'] = "Blog";
$data['list'] = array('first', 'second', 'third');
$data['query'] = $this->blog_model->get_blog_entries();
$this->load->view('blog_view',$data);
}

function comment_display($blog_id)
{
return $this->blog_model->show_comments($blog_id);
}

function comment_insert()
{
$this->db->insert('comments', $_POST);
redirect('blog');
}
}
?&gt;

my model is
&lt;?php
Class Blog_model extends Model
{
function get_blog_entries()
{
return $this->db->get('entries');
}
function show_comments($row_id)
{
$this->db->where('row_id',$row_id)->get('comments');
}
function insert_comment($data)
{
$this->db->insert('comments',$data);
}
}
?&gt;

i am getting the error

( ! ) Fatal error: Call to undefined method CI_Loader::comment_display() in C:\wamp\www\feeders\system\application\views\blog_view.php on line 21


can any one guide me




Theme © iAndrew 2016 - Forum software by © MyBB