简体   繁体   English

链接到CodeIgniter中的静态页面

[英]Link to static page in CodeIgniter

I'm new to PHP and CodeIgniter, and I'm having trouble setting up the routing of static pages in my CodeIgniter app. 我是PHP和CodeIgniter的新手,在我的CodeIgniter应用程序中设置静态页面的路由时遇到问题。

I have the About Us | Contact Us | Privacy Policy 我有About Us | Contact Us | Privacy Policy About Us | Contact Us | Privacy Policy About Us | Contact Us | Privacy Policy and many other pages in my footer, which when the user clicks, should go to a url like mysite.com\\about mysite.com\\contact mysite.com\\privacy 用户单击时, About Us | Contact Us | Privacy Policy和页脚中的许多其他页面应转到类似于mysite.com\\about mysite.com\\contact mysite.com\\privacy的网址

You get the gist.. 你明白了。

Here's what I've done in the footer div in the views folder 这是我在views文件夹的页脚div中完成的操作

<a href="home/staticpages/about">About</a> |
<a href="home/staticpages/contact"> Contact Us</a> | 
<a href="home/staticpages">Privacy</a> | 
<a href="home/staticpages">Terms of Use </a>

Here's the function in a controller Home 下面是一个控制器的功能Home

public function staticpages($page)
    {

        if ( ! file_exists(APPPATH.'application/views/'.$page.'.html'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view($page, $data);

   }

The static pages are just pure HTML content, with <p> fields and no dynamic or javascript stuff. 静态页面只是纯HTML内容,带有<p>字段,没有动态或javascript内容。 So I have them saved as .html; 所以我将它们另存为.html; contact.html, about.html and so on. contact.html,about.html等。 I just want to load those pages' content in a contentbody div I have created, between the header and the footer, and that's the only thing that changes in the whole site for whatever the user clicks. 我只想将这些页面的内容加载到我创建的contentbody div中(在页眉和页脚之间),而这对于用户单击的内容在整个网站中都是唯一的更改。

When I goto http://localhost/myapp/home/staticpages/about 当我转到http://localhost/myapp/home/staticpages/about

I get the 我得到了

404 Not Found error The requested URL /dissekted/home/staticpages/about was not found on this server. 404 Not Found error The requested URL /dissekted/home/staticpages/about was not found on this server. 404 Not Found error The requested URL /dissekted/home/staticpages/about was not found on this server.

And it's the one that the browser throws. 这是浏览器抛出的那个。

config your base url 配置您的基本网址

$config['base_url'] = 

load the helper 加载助手

first method 第一种方法

   $this->load->helper('url');

or second method autoload it by changing application/config/autoload.php 或第二种方法通过更改application / config / autoload.php自动加载它

use with 用于

$this->config->base_url();

first check with echo you are getting base url or not 首先用echo检查您是否正在获取基本URL

<?php echo base_url(); ?>

and use with your static link 并与您的静态链接一起使用

<a href="<?php echo base_url(); ?>/static_file "  />static link</a>

ok make this inside in controllers function like 好的,使它在控制器内部像

public function static ()
    { 

    $this->load->view('static.html');
}

Check your base URL in the config file and make sure you have the final / on the path: 检查配置文件中的基本URL,并确保路径上有最终的/

$config['base_url'] = 'http://localhost/myapp/'

Also, I use the anchor() function from the URL Helper to take create my links. 另外,我使用URL Helper中的anchor()函数创建链接。

That takes care of forming the links correctly and takes into account the base URL. 这样可以正确地形成链接并考虑基本URL。

create privacy.php in view folder and use below code to display it.


$this->load->view('page_name'); no need to use extension like static.php.

function privacy() {`enter code here`
        $data['meta_title'] = 'Privacy Policy';
        $data['title'] = 'Privacy Policy';
        $data['main'] = 'root/privacy';
        $this->load->vars($data);
        $this->load->view($this->template or page_name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM