【问题标题】:Codeigniter wrong url to redirect 404Codeigniter 错误的 url 重定向 404
【发布时间】:2018-05-09 13:12:02
【问题描述】:

我想在输入错误的url时显示404页面

真实网址

https://localhost.com/posts/hello_world

错误网址

https://localhost.com/posts/dsadasd

404 的错误网址

控制器

public function read($slug_posts)
{
    $options    = $this->options_model->listing();

    $posts      = $this->posts_model->read($slug_posts);
    #$listing   = $this->posts_model->home();

    $data = array(  'title'         => $posts->post_title.' – '.$options->option_title,
                    'description'   => $posts->post_description,
                    'posts'         => $posts,
                    #'listing'      => $listing,
                    'content'           =>  'posts/read'
                    );

    $this->load->view('layout/wrapper', $data, FALSE);
}

【问题讨论】:

  • 我们不能坐在这里为你写代码。如果您提供您当前的立场以及您所做的一切,这对您和我们都会有所帮助。
  • 您目前如何解决这个问题?如果您相应地编辑您的问题,您可能会收到一些反馈,例如,您的控制器 posts 的外观如何?
  • if( $post == 'wrong' ) show_404();
  • @BrianGottier 不工作 :(
  • @Vickel 添加控制器

标签: codeigniter controller http-status-code-404


【解决方案1】:

试试这个

public function read($slug_posts)
{
    $options    = $this->options_model->listing();

     $posts      = $this->posts_model->read($slug_posts);
     if ( ! $posts) 
     {
         show_404();
     }
     #$listing   = $this->posts_model->home();

     $data = array(  'title'         => $posts->post_title.' – '.$options->option_title,
                     'description'   => $posts->post_description,
                     'posts'         => $posts,
                     #'listing'      => $listing,
                     'content'           =>  'posts/read'
                     );

    $this->load->view('layout/wrapper', $data, FALSE);
}

如果真的明白你的问题是什么,这个功能会根据你通过url得到的slug在没有帖子时显示404页面。

【讨论】:

    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2013-11-26
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    相关资源
    最近更新 更多