dongmuyuan3046 2016-07-02 14:33
浏览 106
已采纳

如何在codeigniter中路由请求,以便我们可以提供静态css,js和img文件?

I am using codeigniter framework for a project I have, but I have a small issue I can't solve. I want to separate css styling and js scripts from the views, so I have them in separate folders, /css and /js respectively, both are in the assets folder which is in the root folder of my project. The overall structure is like this:

/project-folder

----/application

----/system

----/assets

--------/css

--------/js

----index.php

Now, I have a controller called MyLibrary that has a method called newentry, which when it gets called loads the newentry view.

<?php 
class MyLibrary extends CI_Controller
{
    public function newentry()
    {
    //The upload form
    $this->load->view('newentry');
    }
}

The newentry view is a simple upload form that uses a style.css and script.js for styling and validation etc... I am using the base_url() method of codeigniter to include the path and file name of the files above.

<html>
<head>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/css/style.css">
    <title><?php echo $title; ?></title>
</head>

<body>
 <form class = "dropezone" action = 'upload' method = 'POST' enctype = 'multipart/form-data'>
    <input type="file" name="document" />
    <input type="submit"/>
</form>
</body>
</html>

This line is where I have a problem: href="assets/css/style.css">

the generated string is the correct path, meaning localhost/project-name/assets/css/styles.css, however, the request gets made to this url:

http://localhost/project-name/MyLibrary/localhost/test-pages/assets/css/style.css

which is of course, not where the files are. Does anyone know why is this happening?

Also, how to fix this? should I add a route by hand? if so, how to make a controller that serves static files?

  • 写回答

2条回答 默认 最新

  • duanji1899 2016-07-03 12:50
    关注

    I have figured out why the request wasn't made to the correct url. So no, it's not about adding the protocol scheme. It has to do with how user agents calculate the base url. Let me explain:

    When we use a link tag in HTML, we specify the href attribute, which is a URI. Now, if it's an absolute path there is no problem really, the resource just get's fetched, however, there is a little problem with relative paths, like the one I use. You see, the user-agent or the browser is programmed to append that href to a base url and there's different ways how they calculate that base url, here's what W3C has to say:

    User agents must calculate the base URI for resolving relative URIs according to [RFC1808], section 3. The following describes how [RFC1808] applies specifically to HTML.

    User agents must calculate the base URI according to the following precedences (highest priority to lowest):

    1. The base URI is set by the BASE element.
    2. The base URI is given by meta data discovered during a protocol interaction, such as an HTTP header (see [RFC2616]).
    3. By default, the base URI is that of the current document. Not all HTML documents have a base URI (e.g., a valid HTML document may appear in an email and may not be designated by a URI). Such HTML documents are considered erroneous if they contain relative URIs and rely on a default base URI.

    so, the solution was simple enough after reading this passage, I just had to specify the base url to be used, in the HTML document with the base tag, like this:

    <html>
        <head>
            <title>Home Page - wellcom</title>
            <base href = "<?php echo base_url(); ?>">
            <link rel="stylesheet" type="text/css" href="assets/css/style.css">
        </head>
    
        <body>
            <h3>HANIX</h3>
        </body>
    </html>
    

    and it worked like magic.

    Anyway, thank you for your replies, I hope this answer will be helpful to others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?