简体   繁体   English

Kohana/PHP/Ajax/ 如何调用控制器并返回 HTML 或部分

[英]Kohana/PHP/Ajax/ How to call controller and return HTML or a partial

Full disclosure, I haven't worked with Ajax before.完全公开,我以前从未与 Ajax 合作过。 I am starting to read up on guides but time constraints have me panicking and looking here for help.我开始阅读指南,但时间限制让我感到恐慌并在这里寻求帮助。

Working with Kohana and am trying to figure out how to implement an infinite scroll.与 Kohana 合作,并试图弄清楚如何实现无限滚动。 The application has existing pagination set up.该应用程序已设置现有分页。

I've found that by doing this in my script:我发现通过在我的脚本中执行此操作:

$.post('mycontroller/infinite', {page:page}, function(data) {
    $('.mycontainer').append(data);
});

and setting up the action in my controller:并在我的控制器中设置操作:

<?php
public function action_infinite(){
?><p>HELLO WORLD</p><?php
}
?>

The entire page is being appended to my div after the Hello World text.在 Hello World 文本之后,整个页面都被附加到我的 div 中。 I set up an alert to see what was in data and it was literally the entire page of HTML starting with the p tag Hello World.我设置了一个警报来查看数据中的内容,它实际上是以 p 标签 Hello World 开头的整个 HTML 页面。

How do I go about returning a partial from my controller and not appending the entire page?如何从我的控制器返回部分而不是附加整个页面?

There are two ways to do infinite scrolling:有两种方法可以进行无限滚动:

  1. Return HTML that you can paste directly into the page返回可以直接粘贴到页面中的 HTML
  2. Return JSON data that you then use to generate and append HTML with Javascript返回 JSON 数据,然后您使用这些数据通过 Javascript 生成和附加 HTML

The first approach has the advantage that you don't need to rewrite your views twice.第一种方法的优点是您不需要两次重写视图。 Assuming you're using plain old PHP templates, your code would go something like this:假设您使用的是普通的旧 PHP 模板,您的代码将如下所示:

Product page view:产品页面视图:

<html>
<!-- blah blah blah -->
<div id="products">
<?php echo View::factory('products/page')->bind('page_num', $page)->bind('products', $products) ?>
</div>
<!-- blah blah -->

Page view页面预览

<?php foreach($products as $product): ?>
    <div class="product"><!-- blah blah --></div>
<?php endforeach ?>

Page fetch controller页面获取控制器

public function action_page()
{
    $this->template->content = View::factory('products/page')->bind(...);
}

The main point is to separate the product page code from the actual code that renders the individual products.要点是将产品页面代码与呈现单个产品的实际代码分开。 That way you can easily invoke only the product render code for things like infinite scrolling.这样您就可以轻松地仅调用产品渲染代码来进行无限滚动等操作。

Note: I haven't used Kohana in years so could have made some minor mistakes.注意:我多年没有使用 Kohana,所以可能会犯一些小错误。

OK so my main problem was that the entire page was in the response.好的,所以我的主要问题是整个页面都在响应中。 I found out this was a Kohana thing.我发现这是 Kohana 的事情。 By placing this in my controller method:通过将它放在我的控制器方法中:

$this->auto_render=false;

The response ceased to be bloated with HTML I didn't want.响应不再因我不想要的 HTML 而变得臃肿。

Then I simply had to set my View::factory and echo it.然后我只需要设置我的 View::factory 并回显它。

public function action_getjson()
{
    $this->response->headers('Content-Type', 'application/json; charset=utf-8');
    $this->auto_render = false;
    $this->response->body(json_encode(['var' => 'blabla'));
}

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

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