是否有CakePHP组件/插件为ajax页面提供永久链接? [英] Is there a CakePHP component/plugin that does permanent links for ajax pages?

查看:79
本文介绍了是否有CakePHP组件/插件为ajax页面提供永久链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JsHelper编写JS链接以使用ajax加载页面.像这样:

I'm using the JsHelper to write JS links to use ajax to load in a page. Like this:

<?= $this->Js->link($item['Series']['title'], array(
    'controller' => 'series',
    'action' => 'view',
    $item['Series']['id']
), array('update' => '#menu-items')); ?>

这工作正常,但是现在我想添加它,以便您可以链接到这些页面.因此,例如,当您单击链接时,它会将您重定向到example.com/#!/series/2.然后,您也可以转到该URL,并将正确的页面加载到#menu-items div中.

This is working fine, but now I'd like to add it so that you can link to these pages. So, for example, when you click the link it would redirect you to example.com/#!/series/2. You could then also go to this url and the correct page would be loaded in the #menu-items div.

这是我必须从头开始编写的东西吗,还是有CakePHP帮助程序或插件可以为我做这件事?我以前有做过这样的事情的经验,但是从不使用CakePHP,也不确定从哪里开始.

Is this something I'd have to write from scratch, or is there a CakePHP helper or plugin available that would do it for me? I have experience in doing something like this before, but never with CakePHP and unsure where to start.

谢谢

推荐答案

我现在亲自解决了这个问题,因为我认为由于没有响应,因此尚未为CakePHP构建类似的东西.

I've had a go at tackling it myself now, as I figured that as there was no response then something like this wasn't already built for CakePHP.

首先,javascript:

So first, the javascript:

var hash = '';
window.setInterval(function() {
    if(hash != location.hash) {
        hash = location.hash;

        if(hash.indexOf('#!/') != -1) {
            var url = hash.substr(2);

            $('#my-div').load(url);
        }
    }
}, 300);

此操作是检查location.hash是否已更改,是否已更改,然后检查是否以#!/开头. (即#!/:controller/:action/:id是我要查找的哈希).然后,它仅调用jQuery load函数来加载该控制器和动作.

What this does is check to see if the location.hash has changed, if it has, then it checks to see if it starts with #!/. (I.e #!/:controller/:action/:id is the hash that I'm looking for). It then just calls the jQuery load function to load that controller and action.

然后,我不得不修改链接以使用HtmlHelper中的url方法.

I then had to modify my links to use the url method in the HtmlHelper.

<?
$hashUrl = $this->Html->url(array(
    'controller' => 'categories',
    'action' => 'view',
    $category['Category']['id']
));

echo $this->Html->link($category['Category']['title'], '#!' . $hashUrl);
?>

这将为控制器和操作创建一个字符串,然后将其附加到link方法中的#!.这样做似乎很麻烦(您可能编写了一个自定义帮助程序使其成为一行),但是稍后它允许您更改config/routes.php中的URL.您最终得到的URL是这样的:#!/categories/view/2

This creates a string for the controller and action, then appends it to #! in the link method. Doing it this way, may seem quite long winded (You could probably write a custom helper to make it one line) but it allows you to change the URLs in the config/routes.php later on. You end up with a url like this: #!/categories/view/2

最后,您需要确保在您使用的每种方法的末尾,在控制器中都具有此功能.

Lastly, you need to make sure that in your controllers you have this at the end of each of method you use.

$this->render('view', 'ajax');

我没有想到这是一个完美的解决方案,但是目前它做得很好.也欢迎提出改进建议.

I'm not imagining that this is a perfect solution, but it does the job quite nicely at the moment. Open to suggestions for improvement too.

这篇关于是否有CakePHP组件/插件为ajax页面提供永久链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆