将所有codeigniter函数包含到位于根文件夹中的新静态页面(php文件)[关闭]

将所有codeigniter函数包含到位于根文件夹中的新静态页面(php文件)[关闭]

问题描述:

I am developing a website which having many static pages. so i created controller only for dynamic pages(it might be wrong practice). Keeping all other static files in root folder. Now i want to use base_url() function from codeigniter for the static pages which is located in root folder. But i am not sure im am doing good practice or not. so ** Please give me a solution to manage many static page in codeigniter. **

我正在开发一个拥有许多静态页面的网站。 所以我只为动态页面创建了控制器(这可能是错误的练习)。 将所有其他静态文件保存在根文件夹中。 现在我想使用codeigniter的base_url()函数来处理位于根文件夹中的静态页面。 但我不确定我是否正在做好练习。 所以**请给我一个解决方案来管理codeigniter中的许多静态页面。 ** p> div>

you can create a controller with name Static_page send all your static pages requests to this controller

Static controller

class Static_Page extends CI_Controller{


    function index(){
       $page = $this->uri->segment(1,'default_static_page');
       if (!file_exists(APPPATH."views/{$page}.php"))
         {
           show_404();
         } 
       $this->load->view($page);
   }

}

save all your static pages as views with same name as uri you will call like if you call http://your-site.com/about-us your view will be about-us.php, http://your-site.com/contact your view will be contact.php

It's not as simple as just including the file path to the helper/library that you want. If we use the URL Helper as an example, you'll notice at the top of the file, this line:

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

This means if BASEPATH isn't defined you won't be able to access the file. Assume you have a static page in your root, called my_static_page.php. You could define BASEPATH and include the file, like this:

define('BASEPATH',true);
require_once 'system/helpers/url_helper.php';

This will allow you to access to the script, but it's doesn't necessarily mean that all of the functions will work. For example, the anchor function will work, but the base_url won't. Certain functions may have certain dependencies, requiring functionality from other components of CodeIgniter.

By accessing files directly in the root of your project, you are effectively bypassing CodeIgniter's index.php file. This file will ensure that all of the required paths are setup, that base classes are instantiated, global functions are available etc. CodeIgniter's loading function ($this->load->helper('url');), which would also not be used in this scenario, would load/provide access to the CI instance amongst other things, which you can see is required in the base_url function:

function base_url($uri = '')
{
    $CI =& get_instance();
    return $CI->config->base_url($uri);
}

In short, it would take considerable time and effort to ensure that all of CodeIgniter's functionality is available to other files in the root. It also negates many of the useful features provided by the framework, and the encouraged MVC design pattern. I'd suggest loading your static pages as views, using a controller - you may find this tutorial from CodeIgniter's user guide particularly useful.