PHP MVC从视图调用控制器函数

PHP MVC从视图调用控制器函数,php,scope,visibility,Php,Scope,Visibility,尝试学习PHP MVC。到目前为止一切都很好。我的./controllers/index.php中有这样的函数 <?php class Index extends Controller { function __construct() { parent::__construct(); } function showFeeds() { return 'done'; } } ?> <?php class Ind

尝试学习PHP MVC。到目前为止一切都很好。我的./controllers/index.php中有这样的函数

<?php
class Index extends Controller {

    function __construct() {
        parent::__construct();
    }

    function showFeeds() {
        return 'done';
    }

}
?>
<?php
class Index extends Controller {

    function __construct () {
        parent::__construct ();
    }

    function showFeeds () {
        return 'done';
    }

}
?>
<?php
$_index = new Index ();
$params = $_index -> showFeeds ();
?>

我知道如何调用模型类并在模型类上运行showFeed()。我的问题是如何在我的./views/index.php上打印“done”。有哪些选择

我已经试过下面列出的一些。但是没有运气

  • 父::showFeeds()
  • $this->controller->showFeeds()
    最好使用PHP框架进行MVC

    以下是一些框架:

    ,及

    这些框架使用MVC和自己的语法,因此使用起来非常简单。 我个人使用Codeigniter,它相当简单

    例如,在codeigniter中,您可以执行以下操作:

    控制器:

    function showFeed(){
    
    $data['done'] = 'Done';
    $this->load->view('yourview', $data);
    
    }
    
    视图:


    在MVC中,一般来说,控制器应该处理模型,获取视图,设置视图的数据,然后调用视图或返回视图以呈现视图


    您试图做的是从视图回调控制器。我建议将
    showFeeds()
    移动到模型中。在控制器中,调用模型以获得showFeeds()的结果,并将值传递给视图,最后调用视图进行渲染。

    通常,控制器将生成视图。要生成视图,需要参数,这些参数通常是要显示的数据

    例如,对于HelloWorld控制器,概念如下所示:

    class HelloWorldController extends Controller {
        function indexAction($name) { // The index page of your site for example
            return $this->getView()->render('helloworld.html', array('name' => $name))
        }
    }
    

    然后在您的视图中,您将打印类似“Hello”的内容$name,这是您在渲染函数中传递的名称。

    最后我修复了我的问题

    页面:/controllers/index.php

    <?php
    class Index extends Controller {
    
        function __construct() {
            parent::__construct();
        }
    
        function showFeeds() {
            return 'done';
        }
    
    }
    ?>
    
    <?php
    class Index extends Controller {
    
        function __construct () {
            parent::__construct ();
        }
    
        function showFeeds () {
            return 'done';
        }
    
    }
    ?>
    
    <?php
    $_index = new Index ();
    $params = $_index -> showFeeds ();
    ?>
    
    
    
    页面:./views/index/index.php

    <?php
    class Index extends Controller {
    
        function __construct() {
            parent::__construct();
        }
    
        function showFeeds() {
            return 'done';
        }
    
    }
    ?>
    
    <?php
    class Index extends Controller {
    
        function __construct () {
            parent::__construct ();
        }
    
        function showFeeds () {
            return 'done';
        }
    
    }
    ?>
    
    <?php
    $_index = new Index ();
    $params = $_index -> showFeeds ();
    ?>
    
    
    
    为什么视图会调用控制器上的方法?这是哪个框架?codeigniter?我知道这个主意。我做了我认为正确的事。我错了吗?是否有其他方法可以在my./view/index.phpI中打印showFeed()返回值。我自己的MVC。通过以下视频教程,我学会了创建自己的mvc。但是他没有教这个问题。那是一个非常糟糕的教程=/非常感谢你的帮助,但它给了我这个错误。致命错误:在C:\xampp\htdocs\framework\views\index\index.php第23行的非对象上调用成员函数showFeeds()。如果没有名为showFeeds()的模型,则会出现此错误。创建一个名为showFeeds的模型函数,错误就消失了。