简体   繁体   English

PHP / CodeIgniter中的GET和POST函数

[英]GET and POST functions in PHP / CodeIgniter

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. 我非常喜欢MVC,并且正在尝试向自己教授当今所有主要Web语言中的MVC体系结构框架。

I am currently on CodeIgniter and PHP. 我目前正在使用CodeIgniter和PHP。 I searched online for a way to make the same function behave different for a POST and GET but couldn't find anything. 我在网上搜索了一种方法,以使POST和GET的功能相同,但找不到任何东西。 Does CodeIgniter have this feature? CodeIgniter是否具有此功能?

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: 如果您使用过Ruby On Rails或ASP.NET MVC,您就会知道我在说什么,在这些框架中,我们可以这样做:

[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? 如何在CodeIgniter中实现同一目标?

Thanks 谢谢

Am I missing something? 我想念什么吗? How can I achieve the same thing in CodeIgniter? 如何在CodeIgniter中实现同一目标?

if you want to learn how to truly approach MVC in PHP, you can learn it from Tom Butler articles 如果您想学习如何在PHP中真正使用MVC,可以从Tom Butler的文章中学习

CodeIgniter implements Model-View-Presenter pattern, not MVC (even if it says so). CodeIgniter实现Model-View-Presenter模式,而不是MVC(即使如此)。 If you want to implement a truly MVC-like application, you're on the wrong track. 如果您想实现一个真正的类似于MVC的应用程序,那么您走错了路。

In MVP: 在MVP中:

  • View can be a class or a html template. 视图可以是类或html模板。 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: 在MVC中:

  • Views are not HTML templates, but classes which are responsible for presentation logic 视图不是HTML模板,而是负责表示逻辑的类
  • A View has direct access to a Model 视图可以直接访问模型
  • A Controller should not generate a response, but change model variables (ie assign vars from $_GET or $_POST 控制器不应生成响应,而应更改模型变量(即从$_GET$_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();

The parameters only allow you to retrieve GET variables. 这些参数仅允许您检索GET变量。 If you want to get the POST variables, you need to use the Input library which is automatically loaded by CodeIgniter: 如果要获取POST变量,则需要使用由CodeIgniter自动加载的Input库:

$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: 请注意,您还可以使用此库来获取GETCOOKIESERVER变量:

$this->input->get('data');
$this->input->server('data');
$this->input->cookie('data');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM