Should I call redirect () from my controller or model within MVC?

I am using the MVC framework of PHP Codeigniter, and I have a direct question about where to call the redirect (): Controller or Model?

Scenario: A
user goes to www.example.com/item/555. In my model, I am looking for an element database for the element with identifier 555. If I find the element, I will return the result to my controller. However, if the item is not found, I want to redirect the user somewhere. Should this call redirect () come from a model or controller? Why?

+3
source share
2 answers

, false, :

class SampleModel extends Model
{
    //Construct

    public function FetchItem($id)
    {
        $result = $this->db->select("*")->from("table")->where("item_id",$id)->get();
        if($result->num_rows() == 0)
        {
             return false;
        }
        //return result
    }
}

:

function item($id)
{
     $Item = $this->SampleModel->FetchItem($id);

     if(!$Item)
     {
          redirect("class/error/no_item");
     }
}

, key/value .

/ .

, , / , .

+6

, , , .

, , , , , , - .

, , " "? , , ?

, , ... , , ...

+1

Source: https://habr.com/ru/post/1783566/


All Articles