Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>So first, the javascript:</p> <pre><code>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); </code></pre> <p>What this does is check to see if the <code>location.hash</code> has changed, if it has, then it checks to see if it starts with <code>#!/</code>. (I.e <code>#!/:controller/:action/:id</code> is the hash that I'm looking for). It then just calls the jQuery load function to load that controller and action.</p> <p>I then had to modify my links to use the <code>url</code> method in the <code>HtmlHelper</code>.</p> <pre><code>&lt;? $hashUrl = $this-&gt;Html-&gt;url(array( 'controller' =&gt; 'categories', 'action' =&gt; 'view', $category['Category']['id'] )); echo $this-&gt;Html-&gt;link($category['Category']['title'], '#!' . $hashUrl); ?&gt; </code></pre> <p>This creates a string for the controller and action, then appends it to <code>#!</code> in the <code>link</code> 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 <code>config/routes.php</code> later on. You end up with a url like this: <code>#!/categories/view/2</code></p> <p>Lastly, you need to make sure that in your controllers you have this at the end of each of method you use.</p> <pre><code>$this-&gt;render('view', 'ajax'); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload