2

I like MVC (a lot), and I am trying to teach myself a framework of MVC architecture in all the major web languages of today.

I am currently on CodeIgniter and PHP. I searched online for a way to make the same function behave different for a POST and GET but couldn't find anything. Does CodeIgniter have this feature?

If you've used Ruby On Rails or ASP.NET MVC you'll know what I'm talking about, in them frameworks we can do this:

[GET]
public ActionResult Edit(int Id)
{
    // logic here for GET
}

[POST]
public ActionResult Edit(EntityX EX)
{
    // logic here for POST
}

I am so used to this, that I am finding it hard wrapping my head around how to get the same smooth functionality without that useful ability.

Am I missing something? How can I achieve the same thing in CodeIgniter?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
J86
  • 14,345
  • 47
  • 130
  • 228
  • mean you want to check get and post using codeignitor? – Praveen kalal Apr 29 '13 at 08:56
  • No I know it can check the GET and POST like a regular PHP program can. But I was specifically asking about having two functions with the same name in the same controller. One will run when the request is of type GET, and the other when the request is of type POST. – J86 Apr 29 '13 at 09:01
  • 2
    You cannot. It's a limitation in the naming convention for CI "controller" actions. – tereško Apr 29 '13 at 09:09

2 Answers2

5

Am I missing something? How can I achieve the same thing in CodeIgniter?

if you want to learn how to truly approach MVC in PHP, you can learn it from Tom Butler articles

CodeIgniter implements Model-View-Presenter pattern, not MVC (even if it says so). If you want to implement a truly MVC-like application, you're on the wrong track.

In MVP:

  • View can be a class or a html template. View should never be aware of a Model.
  • View should never contain business logic
  • A Presenter is just a glue between a View and the Model. Its also responsible for generating output.

Note: A model should never be singular class. Its a number of classes. I'll call it as "Model" just for demonstration.

So it looks like as:

class Presenter
{
    public function __construct(Model $model, View $view)
    {
       $this->model = $model;
       $this->view = $view;
    }

    public function indexAction()
    {
         $data = $this->model->fetchSomeData();

         $this->view->setSomeData($data);

         echo $this->view->render();
    } 
}

In MVC:

  • Views are not HTML templates, but classes which are responsible for presentation logic
  • A View has direct access to a Model
  • A Controller should not generate a response, but change model variables (i.e assign vars from $_GET or $_POST
  • A controller should not be aware of a view

For example,

class View
{
   public function __construct(Model $model)
   {
       $this->model = $model;
   }

   public function render()
   {
      ob_start();

      $vars = $this->model->fetchSomeStuff();

      extract($vars);

      require('/template.phtml');

      return ob_get_clean();
   }
}

class Controller
{
    public function __construct(Model $model)
    {
      $this->model = $model;
    }

    public function indexAction()
    {
        $this->model->setVars($_POST); // or something like that
    }
}

$model = new Model();
$view = new View($model);

$controller = new Controller($model);

$controller->indexAction();

echo $view->render();
Yang
  • 8,580
  • 8
  • 33
  • 58
  • I don't know who Tom Butler, though I found his blog and I ended up here, is but he says the opposite doesn't he? I wish he could/would "debate" about it. Where he'd do that I don't know. – johnny Aug 25 '14 at 13:39
  • 1
    johnny: Email me. bad_boy is right though, his example has the application state (e.g "what is the id of the record being edited, or what is the search term for the results being displayed) stored in the model and has the view then getting its data from the model rather than being fed it by the controller. The data-flow in the example above is correct. – Tom B Jan 15 '15 at 10:33
2

The parameters only allow you to retrieve GET variables. If you want to get the POST variables, you need to use the Input library which is automatically loaded by CodeIgniter:

$this->input->post('data');

So, in your case, it would be:

public function edit($id = -1)
{
    if($id >= 0 && is_numeric($id))
    {
        // logic here for GET using $id
    }
    else if($id === -1 && $this->input->post('id') !== false)
    {
        // logic here for POST using $this->input->post('id')
    }
}

Note that you can also use this library to obtain GET, COOKIE and SERVER variables:

$this->input->get('data');
$this->input->server('data');
$this->input->cookie('data');
Tristan Bourvon
  • 368
  • 2
  • 7