How should I trigger a 404 error from template logic

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
nhaack

How should I trigger a 404 error from template logic

Post by nhaack »

Hi there,

Imagine the following scenario:

I have a tag that fetches data and provides it as smarty-object or returns false if it is unable to solve a given problem. That works great.

The tag (plug-in) is called with a parameter param which can have a limited number of some string values (~1,000).

I use $smarty.get.param in the plug-in call (simplified)

Code: Select all


{plug_in param=$smarty.get.param}

So I call my page with: http://example.com/page-with-plug-in/param-value

and map it to the following scheme (simplified)

Code: Select all


RewriteCond %{REQUEST_URI} ^/page-with-plugin
RewriteRule ^([^/]*)/(.+) index.php?page=$1param=$2 [QSA,L]

Within the page "page-with-plug-in" I have the following smarty logic

{plug_in param=$smarty.get.param}

{if $plug_in_result == false}
... this entry does not exist
{else}
... do some stuff with the data
{/if}

This setup works so far. However, the page will always return a status code 200, since the page itself exists and is working correctly. But now, I'd like to send the 404 Error page in case, the plug-in returns a false.

I could successfully return a 404 header with a one-liner UDT. So now I can display some error message and send a 404 header but still it doesn't feel "smooth".

Code: Select all


header("HTTP/1.1 404 Not Found");

So my template finally looks something like that:

Code: Select all


<__html>
...
</__body ... >

{plug_in param=$smarty.get.param}

{if $plug_in_result == false}
 {tag_send_404_header}
 <h1>404 Error</h1>
{else}
 <h1>{$plug_in_result->data_1}</h1>
{/if}

...

{if $plug_in_result == false}
 <div>{global_content name='error_404_message'}</div>
{else}
 <div>{$plug_in_result->data_2}</div>
{/if}

<__body>
</__html>

There are several templates in this site that have similar functionality so I use a global content block to reduce maintenance time on the error page but I also don't perfectly like this.

Ideally, would like to make CMSMS stop rendering the page and return the defined 404 error instead.

I could theoretically conduct a redirect, but this also doesn't sound right as a false result.

It's not that I couldn't live without triggering the CMSMS 404. But I'm still curious whether I can call the 404 via a specific given way.

At the moment I can think of a UDT than fetches the 404 Error data. But that wouldn't be much different.

What could be another approach for a coherent 404 behavior to the visitor?

I'm looking forward to your ideas.

Best
Nils
User avatar
Augustas
Forum Members
Forum Members
Posts: 241
Joined: Wed Oct 17, 2007 6:09 pm
Location: the world

Re: How should I trigger a 404 error from template logic

Post by Augustas »

Hi Nils,
have you found solutions to your problem?
I am in a similar scenario, and looking for possible answers.
http://FollowTheRoad.com/ - living on the road...
http://www.kligys.com/ - asmeninis blog'as...
nhaack

Re: How should I trigger a 404 error from template logic

Post by nhaack »

Hi Augustas,

not really. What I did is the following:

I wrapped the whole template in an IF-Statement checking whether the entry exists or not. If not, I send a 404-Header and pull in some global content blocks that contain the 404-Informationen. This makes the error messages maintainable across multiple templates. Once in place, the solution works pretty fine and seems reliable/stable.

There might be a better solution, but I hadn't much drive to tweak it any further :D

Best
Nils
Post Reply

Return to “Developers Discussion”