如何在 ID 为 null 时显示 404 错误页面?

How can I show 404 error page when the ID is null?

我在 MYSQL 中创建了一个 table。我将一些数据插入 table。

我的问题是关于 ID 为 NULL 时的错误。

当URL 为“.com/blog/post/1”时,内容的ID 1 和其他ID 的内容一样完美。但问题是当 url 为 ".com/blog/post" 时,我无法解决(因为我是 Codeigniter 的新手)发生错误。

我认为 "post/" 需要的问题和 ID 号本身,但如何使用如下所示的页面更改错误

Forbidden

You don't have permission to access /post/ on this server.

您可以将ID作为参数传递给函数;像这样;

public function post($id = false)
{
    if (!$id)
    {
        // No ID supplied
    }
    else
    {
        // ID found.
        // Carry on....
    }
}

希望对您有所帮助。

在您的 blog 控制器中,当有人请求 post 时,您可以检查 id 是否存在,如果不存在,您可以将他们重定向到博客索引。

blog 控制器将如下所示。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {

   function __construct()
   {
       parent::__construct();
   }

   function index()
   {
       //List posts with title and excerpts and a read more link
   }

   function post($id=NULL)
   {
       if($id)
       {
         //Show detailed post
       }
       else
       {
         redirect('blog');
       }
   }
}