“用户控制”使用 MVC PHP

发布于 2024-10-18 01:39:21 字数 427 浏览 2 评论 0原文

我想知道如何在 MVC 框架(特别是 CodeIgniter)中使用 PHP 实现 asp.net“用户控件”之类的功能。

为了更好地解释我想要什么,这里有一些指导:

  • .net 用户控件是一个文件,包含 aspx 和代码 (c#/vb),它在实现它的每个页面(例如,购物车)中提供功能。它可以轻松添加到母版页中,母版页是站点中所有其他页面的容器。

  • 在MVC结构中,页面由控制器加载,控制器加载视图。我知道我可以在视图中加载视图,但是如何为“跨站点视图”提供控制器代码而不在每个主控制器中重复它?

示例:我有一个视图,将数据库中的类别加载到选择列表中,并且该视图位于每个页面的顶部。由于我不应该(而且我不确定是否可能)从视图访问我的类别模型,因此我应该在哪里放置代码来加载此数据,而不必在每个控制器的每个函数中重复它?

I was wondering how to achieve an asp.net "user control" like functionality with PHP in a MVC framework, specifically CodeIgniter.

To explain better what I want, here's some guidance:

  • A .net user control is a file, with both aspx and code (c#/vb) that provides a functionality across every page that implement it (say, a shopping cart). It is easily added to a Master Page which is a container for every other pages in the site.

  • In MVC structure, a page is loaded by the controller, which loads the View. I understand I can load Views inside Views, but how to provide the controller code for "cross site views" without repeating it every main controller?

Example: I have a view that loads Categories from database into a select list and this view is on top of every page. As I shouldn't (and I'm not sure if it is even possible) access my Category model from the view, where do I put the code to load this data without having to repeat it in every function in every controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巨坚强 2024-10-25 01:39:21

如果您想避免在每个控制器中重复代码,那么只需扩展 CI_Controller 类并在实例化时在新的 Controller 类中进行所有设置...并让所有普通控制器继承自 <代码>New_Controller。

If you want to avoid duplicating code in each controller, then simply extend the CI_Controller class and do all of the setup in your new Controller class upon instantiation ... and have all of your normal controllers inherit from New_Controller.

痞味浪人 2024-10-25 01:39:21

正如 Sean 所说,如果功能位于控制器级别,您应该扩展 CI_Controller。
可能某些功能应该在视图级别,因此加载器视图可用于帮助您保持每个页面的全局布局并包含常见功能。类似于:

[view] layout.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
        <?php
            $this->load->view('meta', $data);
        ?>
</head>
    <body>
        <div id="wrapper">

            <?php
            $this->load->view('header', $data);
            ?>
            <div id="contents">

                <?php include ('menu_izq.php'); ?>

                <div id="page">
                    <?php $this->load->view($page, $data); ?>
                    <div class="clear"></div>
                </div> 
            </div>
            <div style="clear: both;"></div>
            <?php $this->load->view('footer');?>
        </div>
    </body>
</html> 

在您的控制器中,您应该始终加载此视图并在参数数组中传递内容真实视图的值,例如

    $data['page'] = 'incidents'; // this is the real contents
    $stylesheets[] = '/scripts/jscalendar-1.0/skins/aqua/theme.css';
    $data['stylesheets'] = $stylesheets;
    $scripts[] = '/scripts/jscalendar-1.0/calendar.js';
    $scripts[] = 'https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
    $scripts[] = '/scripts/autoNumeric-1.4.1.js';
    $scripts[] = '/scripts/autoLoader.js';
    $data['scripts'] = $scripts;
    $this->load->view('container',$data);

As Sean said, if the functionality is at Controller level you should extend CI_Controller.
May be some functionality should be at view level, so a loader view can be used to help you keep the global layout of every page and include the common features. that's something like:

[view] layout.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
        <?php
            $this->load->view('meta', $data);
        ?>
</head>
    <body>
        <div id="wrapper">

            <?php
            $this->load->view('header', $data);
            ?>
            <div id="contents">

                <?php include ('menu_izq.php'); ?>

                <div id="page">
                    <?php $this->load->view($page, $data); ?>
                    <div class="clear"></div>
                </div> 
            </div>
            <div style="clear: both;"></div>
            <?php $this->load->view('footer');?>
        </div>
    </body>
</html> 

In your controller you should always load this view and pass in the parameter array the value of the contents real view, like in

    $data['page'] = 'incidents'; // this is the real contents
    $stylesheets[] = '/scripts/jscalendar-1.0/skins/aqua/theme.css';
    $data['stylesheets'] = $stylesheets;
    $scripts[] = '/scripts/jscalendar-1.0/calendar.js';
    $scripts[] = 'https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
    $scripts[] = '/scripts/autoNumeric-1.4.1.js';
    $scripts[] = '/scripts/autoLoader.js';
    $data['scripts'] = $scripts;
    $this->load->view('container',$data);
煞人兵器 2024-10-25 01:39:21

我很少使用母版页,主要是在 Sharepoint 中,但如果您想要一个 front.master 页面,可以这么说,然后加载该上下文中的所有其他页面,您最好的选择是创建一个新库作为您的视图处理程序并以编程方式设计它以加载主视图中的辅助视图。

一个简单的类,包括:

class MasterViewTemplate
{
    public $master = "v1.php";
    private $data = array();

    public function __set($key,$value)
    {
        $this->data[$key] = $value;
    }

    public function __get($key)
    {
        return $this->data[$key];
    }

    public function setMaster($master)
    {
        $this->master = $master;
    }

    public function display($page)
    {
        //Load the master file
        //Load the inner $page file
        //Inject the page contents into the master
    }
}

这就是您正在寻找的东西吗?

I have worked very little with master pages, mainly in Sharepoint but if you want a front.master page so to speak and then load all other pages within that context your best bet would be to create a new library as your view handler and pro grammatically design it to load the secondary view within the master.

A simple class consisting of:

class MasterViewTemplate
{
    public $master = "v1.php";
    private $data = array();

    public function __set($key,$value)
    {
        $this->data[$key] = $value;
    }

    public function __get($key)
    {
        return $this->data[$key];
    }

    public function setMaster($master)
    {
        $this->master = $master;
    }

    public function display($page)
    {
        //Load the master file
        //Load the inner $page file
        //Inject the page contents into the master
    }
}

IS this the kind of thing you was looking for.

紫竹語嫣☆ 2024-10-25 01:39:21

回答我自己的问题:我发现了一些值得检查的东西。它提供了多个 MVC 三元组: https://bitbucket.org/wiredesignz/codeigniter -modular-extensions-hmvc/wiki/Home

Answering to my own question: i found something worth checking. it provides Multiple MVC triads: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文