October: [feature] 404 header and page from component

Created on 23 May 2014  路  12Comments  路  Source: octobercms/october

i've inspected cms code for search method with return 404 error

i'm found Response::make(View::make('cms::404'), 404);
but this returns system 404

also exists


   if (!$page && !($page = $this->router->findByUrl('/404')))
            return Response::make(View::make('cms::404'), 404);

        $this->page = $page;

but this code doesn't run from component, first variant is better, but cannot shows my 404 page

Most helpful comment

I just need to ask is octobercms has only capable of setting two types of errors ???
1.404 [by creating a page with /404 url ]
2.500 [by creating a page with /error url ]

how can we set 401, 402, 403 etc ??????

All 12 comments

class Something extends ComponentBase
{
    public function onRun()
    {
        return $this->controller->run('404');
    }
}

This allows you to show your own 404 page, or any page you'd like for that matter. The issue is the HTTP status code. This will be HTTP 200 "OK" no matter what. We should have a way of passing a statuscode to the run() method.

Another solution would be to do something like this:

App::abort(404); // triggers NotFoundHttpException

This is Laravel's built in solution. The router or controller (i'm not sure which one) might want to listen for "NotFoundHttpException" and show the default 404 page instead of a fatal error page, automatically setting the status code to 404.

class Something extends ComponentBase
{
    public function onRun()
    {
        return Response::make($this->controller->run('404'), 404);
    }
}

Fair enough. Not the most developer friendly solution, but it floats my boat :-)

image

My code:

title = "Test"
url = "/news/:post_slug"
layout = "default"
==
<?php
    use RainLab\Blog\Models\Post;

    function onStart()
    {
        return Response::make($this->controller->run('404'), 404);
        // $slug = $this->param('post_slug');
        // $this['article'] = Post::isPublished()->where('slug', $slug)->first();
        // if (!$this['article']) {

        // }
    }
?>
==
Test

But this working fine:

return Response::make(View::make('cms::404'), 404);

But,

return Response::make(View::make('cms::404'), 404);

return laraval 404 page, not cms custom 404.htm

return Response::make($this->controller->run('404'), 404);

insert response text in body tag ... (HTTP.....)
What i should to do to display custom 404 page from component correctly?

I'm experiencing this issue when trying to use a 404 in a component. Any ideas?

    public function onRender()
    {
        $this->category = Category::where('slug', $this->param('category'))->get()->first();
        if(!$this->category['id']) {
            $this->setStatusCode(404);
            return $this->controller->run('404');
        }

Not only is the header status being displayed, but the entire 404 page is being injected into {% page %}, layout and all.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Missing Page </title>
...
<body>
<div class="main-container">

HTTP/1.0 404 Not Found
Cache-Control: no-cache
Date:          Sun, 10 Sep 2017 22:10:26 GMT

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Missing Page </title>

@buckhornmktg That's because you're doing that in onRender(). Do that in onRun() instead and it will work fine.

Doh! Thanks Luke, that was the problem.

Not a problem, happy to help.

I just need to ask is octobercms has only capable of setting two types of errors ???
1.404 [by creating a page with /404 url ]
2.500 [by creating a page with /error url ]

how can we set 401, 402, 403 etc ??????

public function onRun(){
    return $this->controller->setStatusCode(404)->run(404);
}
Was this page helpful?
0 / 5 - 0 ratings