Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use CodeIgniter with Smarty. I have an additional function in my Smarty class called notfound(). Calling notfound() sets the correct header location to a 404 page and then displays a 404 template. The template has an overrideable title and message so it's very versatile. Here's some sample code:</p> <pre><code>Smarty.class.php function not_found() { header('HTTP/1.1 404 Not Found'); if (!$this-&gt;get_template_vars('page_title')) { $this-&gt;assign('page_title', 'Page not found'); } $this-&gt;display('not-found.tpl'); exit; } </code></pre> <p>In a controller I can do something like this:</p> <pre><code>$this-&gt;load-&gt;model('article_model'); $article = $this-&gt;article_model-&gt;get_latest(); if ($article) { $this-&gt;smarty-&gt;assign('article', $article); $this-&gt;smarty-&gt;view('article'); } else { $this-&gt;smarty-&gt;assign('title', Article not found'); $this-&gt;smarty-&gt;not_found(); } </code></pre> <p>Likewise, I can change the code in /system/application/error/error_404.php to:</p> <pre><code>$CI =&amp; get_instance(); $CI-&gt;cismarty-&gt;not_found(); </code></pre> <p>It works great, uses a small amount of code, and doesn't duplicate 404 functionality for different types of missing entities.</p> <p>I think you could do a similar thing with the built-in CodeIgniter views. The important thing is to spit out the header before you do your view stuff.</p> <p>Update: I use a custom Smarty wrapper similar to the one described here:</p> <p><a href="http://devcha.blogspot.com/2007/12/smarty-as-template-engine-in-code.html" rel="nofollow noreferrer">Using Smarty with CodeIgniter</a></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