php CI webapp fails with 404 while trying to load css file - php

I have the following code in a view for my codeigniter application:
<link href="<?php echo APPPATH.'assets/css/bootstrap.min.css'?>" rel="stylesheet">
This is giving me a 404.
However, a few lines down, I tried this to troubleshoot:
<?php echo APPPATH.'assets/css/bootstrap.min.css' ?>
That shows the following path on my webpage:
/var/www/html/testapp/application/assets/css/bootstrap.min.css
If I copy that path and paste it into a new browser window, it returns the contents of my css file.
I'm not sure where I've gone wrong.
Tips?

Try
<link href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet">
(In response to the helpful comment)
base_url() requires the URL Helper to be loaded which can be done one of two ways.
Loaded in a controller
$this->load->helper('url');
Or, set to autoload in /application/config/autoload.php
$autoload['helper'] = array('url');

If the path you get printed by the 2nd line you've given is correct, then it might be the slash at the beginning. So instead of /var/... etc. try make sure it's var/... etc. Solution to your problem might be then as follows:
<?php
$str = APPATH.'assets/css/bootstrap.min.css';
echo substr($str, 1);
?>
The second variable at the substr is the startposition of the new string, so that should get rid of the slash at the beginning. Hope this helps!

Related

How to make Dynamic css path?

I'm using not_found.php file for unfound files on server , and it's in Main Directory
it's style is in 'assests/css/main.css' from same Directory ,
so I make href of linking stylesheet
href="assests/css/main.css"
it works perfect if user types 'Main/wrongFile' for example , but if user typed 'Main/ez/wrongFile' , stylesheet is included wrongly due to changing of path
I'm searching for function like
$Path = FindPath('Main/assests/css/main.css');
so it gets that path wherever file is in
then I can simply
href="<?php echo $Path; ?>"
The best way to fix it is using the code below;
<link rel="stylesheet" href="/assests/css/main.css" type="text/css"/>
adding / at beginning of assests/css/main.css simply tells browsers which root directory to look at.
Option 2) Using htaccess. Add the code below into your .htaccess file
RewriteRule ^([^/]*)/assests/css/main.css$ assests/css/main.css [R=301,L,NC]

how to remove codeigniter functions from base url when routing

I routed my controller function and suddenly the function name is added to base URL, how can I remove this?
$route['editblog/(:num)']='Code/editblog/$1';
Then all of the sudden edit blogs are added to base URL and my external files are showing an error.
<script src="assests/vendor/slick/slick.min.js">
It shows:
<script src="editblog/assests/vendor/slick/slick.min.js">
Please help me, help will be highly appreciated!
You need to verify 1 things:
application/config.php $config['base_url'] = 'application/path/'; like http://example.com/ or http://localhost/applicationName/
Now you should use urls like
base_url('assets/js/filename/js');
base_url('controller/action/parameters');
In this way, it will generate correct urls.
You should change your URL's to:
<script src="<?php echo base_url('assests/vendor/slick/slick.min.js'); ?>">
Actually base_url is set in your applications/config.php file. -> config['base_url']. It MUST include trailing slash
in your config file:
$config['base_url'] = 'http://test.com';
when you call out the base_url(). You can use this :
<script src="<?= base_url().'/assests/vendor/slick/slick.min.js'; ?>">

Why won't this PHP code feed in to the link TAG href attribute? (local host, not using laravel)

Disclaimer: I haven't got a clue what I'm doing with PHP I'm just playing around with it.
I have my css file in a folder named CSS and then my header.php and footer.php in the main site folder. If i include the header.php in other directories I am just using:
<?php include('../header.php'); ?>
I know this isn't the way to do it however I don't know how to configure it probably (with a config.php file etc..) but my issue is, once the header's included in files in any directory of course it will look for the css/main.css file in that folder so I've tried doing the following:
<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']. '/JAGS/css/main.css' ?>" />
When I use the php line in the body it displays the path
E:/xampp/htdocs/JAGS/css/main.css
but if I use it there in the link tag then doesn't work.
What seems to be my problem other than the fact I'm clueless with PHP. Is there something else I should be using? Is there something I need to do in my xampp config files?
Edit: By "doesn't work" I mean the styles are not being applied.
Edit 2: Inspecting shows the following:
<link rel="stylesheet" href="E:/xamppp/htdocs/JAGS/css/main.css">
I know there's an extra p on the end of xampp, this is actually what I have the folder named. Is it because it's not saying "localhost/JAGS/CSS/main.css"? If so what would be the reason for this?
Edit 3: Console shows error below:
Not allowed to load local resource: file:///E:/xamppp/htdocs/JAGS/css/main.css
Edit 4: Not using Laravel
Thank you
Why do you require the document root, you should just place a dot in front of it and set the base url in a meta tag.
<base href="yourdomain.com">
<link rel="stylesheet" href="./JAGS/css/main.css'/>

Setting a variable but php is just ignoring it and overwriting it?

Okay, so, I have a configuration.php, and in there I have this:
$baseURL = 'http://www.website.com'; (just hiding my site)
On every page I call the configuration.php, and it doesn't matter what directory or page I'm on (yes I'm setting the configuration.php to the correct directory) but I try to make a image link like this:
<img src="<?php echo $baseURL; ?>/images/logo.png">
So that should, when clicked, take me to http://www.website.com. Nope, it takes me to the same page. It's like its overwriting itself... I have nothing overwriting it in my code, it just like sets itself to the current page instead of just http://www.website.com
I'm not sure why you have a config file to declare the value, and by call, I'm guessing you mean include. If you're not using include, could you tell us what you're doing? More code!
config.php
<? $baseURL = "http://" . $_SERVER['SERVER_NAME']; ?>
Then
<? include "config.php"; ?>
<img src="<? echo $baseURL; ?>/images/logo.png" alt="logo.png"><? echo $baseURL; ?>
Also check for error_log entries related to this, you may find that it tells you exactly what is wrong.

How can I include the CSS file in CodeIgniter?

I have make a assets folder in root directory where application folder exist. now I have assets sibling to application.
Now when I tried to open http://localhost/CodeIgniter/assets/css/bootstrap.min.css in web-page it's not working.
Do someone can help me how I can got it worked.
In a solution I found that writing this line in config file will work but it's not work either by doing this change.
$config['base_url'] = 'http://localhost/CodeIgniter/';
Now please some guideline me how to load CSS file in CI.
solution :-
I have done something wrong in my codeigniter project. I download it again and try these thing again, without anything it's working.
thanks you all of guys. you save my time.
In constant.php:
define('URL','http://localhost/CodeIgniter/');
define('IMG',URL.'assets/img/');
define('CSS',URL.'assets/css/');
define('JS',URL.'assets/js/');
In config.php:
$config['base_url'] = URL;
In view:
<link rel="stylesheet" href="<?php echo(CSS.'bootstrap.min.css'); ?>">
Hope that helps somewhat.
define general css that used.
Open "config.php" within directory CodeIgniter\system\application\config.
$config['css'] = 'mystyles.css';
Create a file named mystyles.css within root application: \CodeIgniter.
$this->load->helper('url');
$data['css'] = $this->config->item('css');
You can load css in views pages like this.
Add below line in your controller action /application/controllers/yourController.php
$this->load->helper('url');
Then add below line to your view file's head tag.
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/yourcssfile.css');?>" />
Assuming that you have assets/css/ folder is created in your app directory.
<path to your app folder>/assets/css

Categories