简体   繁体   中英

Need help accessing the assets folder in Code Igniter 3

I am working with Code Igniter 3. I kept all the assets file in the root folder like I normally do. However, I needed to move all the files inside a different folder, let me explain:

BEFORE

user.php was in : %root%\\application\\views\\

admin-panel.php was in : %root%\\application\\views\\

And the assets in: %root%\\assets

NOW

user.php file is inside a folder: %root%\\application\\views\\usr\\

admin-panel.php was in : %root%\\application\\views\\adm

And the assets is in: %root%\\assets

I know I can access the asset files from adm or the usr folders by simply changing assets/anything.extension to ../assets/anything.extension

Or by copy-pasting the assets folder in both the directories (which I don't want to do)

However, I have already finished the designs and it will be very time consuming to do that so I would like to know if there's any other way to let the files in adm or usr access the files inside assets by maybe using route.php or .htaccess something?

you need to add base path and assets path in config file .

i am providing some configuration . Follow these steps

Go-To application\\config\\config.php

add these code ( configure base url and assets path )

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

define('ASSET_PATH',$config['base_url'].'assets/');

and finally you can used in link like this

<a href="<?php echo base_url('index.php/yourController/yourFunction'); ?>">Any Link</a>

for assets (like img )

<img src="<?php echo base_url(); ?>assets/img/logo.png" alt="">

You can get js , css files like this

<link href="<?php echo ASSET_PATH; ?>css/bootstrap.min.css" rel="stylesheet">

<script src="<?php echo ASSET_PATH; ?>js/jquery.min.js"></script>

Well as your assets folder is in the same folder as index.php which is where the paths have to be "relative" to... You can just get away with something like

<img src="/assets/img/logo.png" alt="">

Using HTACCESS Now I must state that I am only just dangerous when it comes to htaccess stuff.

Trying to redirect assets to /assets seems to mess things up on my server. What I did find that if you rename your assets folder to asset or even move it, this appears to work. So not sure of any side effects this might have but it seems to play ok on my test site.

.htaccess

RewriteEngine On

# Note - the system assets folder has been renamed to asset
# to make it different. It doesn't like them being the same
# with this implementation. Especially if we match on /assets or assets.

RewriteRule ^/?assets/?$ /asset/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

The /? in ^/?assets/?$ is just in case you do have a leading / which then forces a rename of the folder from assets to asset. Else it goes chasing its tail in a loop...

There will be better ways to do this, that the smarter folks will know, so it's a possible solution but I'm not claiming its the best or even technically correct.

If you do use it, test the stuffing out of it.

My Test setup.

.htaccess file in root folder

Controller called User.php

class User extends CI_Controller {

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

    public function index() {
        $this->load->view('usr/user_view');
    }
}

View called user_view.php which resides under application/views/usr and contains...

<h1>This is the User View</h1>
<img src="assets/images/alien-icon.jpg" width="300" height="300" alt="">

And the image resides at /asset/images/ NOTE: it is asset and not assets

So any references to assets/stuff... is being redirected to asset/....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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