Codeigniter HMVC 并从 jQuery 调用视图

发布于 2024-11-16 12:45:35 字数 1044 浏览 7 评论 0原文

Codeigniter 中的 HMVC 新增功能。

动态表单允许在用户单击链接时创建新的“段”。

目前分段 html & php 位于 module/view/segment_view.php 中。该视图还需要动态 php 变量。

如何使用 jQuery“加载视图”?我知道我无法直接从 jQuery 加载视图,但我不知道如何为 AJAX 调用构建控制器。看来这可能不是Codeigniter的强项?

编辑:
“段”位于表单内。该表格始终有 1 个段。 jQuery 可以添加更多段。每个段都需要 php 数组来进行表单输入和验证。

这是 form_view 文件的精简版本

for ($i=1; $i<=$number_of_segments; $i++) {

    $data['location'] = array(
        'name'  =>  "segment[$i][location]",
        'value' =>  set_value("segment[$i][location]"),
        'maxlength' =>  '255',
        'size'  =>  '30'
    );  
 ?>
    <div class="area">
        <div class="row"><h2>Segment <?php echo $i;?></h2></div>

        <?php 
        $this->load->view('module/segment_view', $data);
        ?>

    </div> <!--/div class="area"-->
<?php
}    //end for
?>

,所以这并没有真正将应用程序逻辑与表示分离,不是吗?哈哈。那么构造这个代码的最佳方法是什么,以便代码是 HMVC 并且可以从 php 控制器/视图以及 jQuery 访问?

New to HMVC in Codeigniter.

Dynamic form allows new "segments" to be created when a user clicks a link.

Currently the segment html & php is in module/view/segment_view.php. The view requires dynamic php variables as well.

How can I "load the view" using jQuery? I understand I can't directly load a view from jQuery but am lost on how to structure the controller for an AJAX call. Seems this may not be a strength of Codeigniter?

EDIT:
The "segment" is inside a form. The form always has 1 segment. jQuery can add more segments. Each segment requires php arrays for form inputs and validation.

Here is a pared down version of the form_view file

for ($i=1; $i<=$number_of_segments; $i++) {

    $data['location'] = array(
        'name'  =>  "segment[$i][location]",
        'value' =>  set_value("segment[$i][location]"),
        'maxlength' =>  '255',
        'size'  =>  '30'
    );  
 ?>
    <div class="area">
        <div class="row"><h2>Segment <?php echo $i;?></h2></div>

        <?php 
        $this->load->view('module/segment_view', $data);
        ?>

    </div> <!--/div class="area"-->
<?php
}    //end for
?>

OK, so this isn't really separating application logic from presentation, now is it? Ha ha. So what is the best way to structure this so the code is HMVC and can be accessed from a php controller/view as well as jQuery?

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

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

发布评论

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

评论(1

甜警司 2024-11-23 12:45:35

您不能直接通过 URL 调用视图文件,您需要调用加载视图的控制器方法。

如果你想做一些“仅ajax”的事情,你可以使用这个函数:

if ($this->input->is_ajax_request()) // Do ajax stuff

示例是加载视图片段或json。您不必在控制器中创建“仅限 ajax”方法,如果是 ajax 请求,您可以在现有控制器方法中使用此函数来执行不同的操作。

不确定这是否可以为您解决任何问题,请提供一些代码和您想要执行的操作的更多上下文,我们可以为您提供一些更清晰的建议。

You can't call view files directly by URL, you need to call a controller method that loads the view.

If you want to do some "ajax only" stuff, you may use this function:

if ($this->input->is_ajax_request()) // Do ajax stuff

Examples would be loading view fragments or json. You don't have to make "ajax only" methods in your controller, you can use this function from within your existing controller methods to do something different if it's an ajax request.

Not sure if this resolves anything for you, provide some code and more context of what you're trying to do and we can give you some clearer advice.

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