base_url including base address twice.and not loading bootstrap css file - php

new at codeigniter, trying simple code to load bootstrap css files with base_url() function.tried some answers on stackoverflow,but issue not solved.
my view name is.... articles_list.php
my controller name... user.php
i edit config.php as follow 'localhost/ci';
i edit autoload.php as follow i load url helper in autoload.php
articles_list.php (loading css in view file)
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/bootstrap.css') ?>" >
user.php(controller)
$this->load->helper('url');
$this->load->view('public/articles_list');
when i run my view file, css is not loading, when i check view source it should give me both stylesheet url and link address as "localhost/ci/assets/css/bootstrap.css" but when i see view source it show <link href="localhost/ci/assets/css/bootstrap.css"> and when mouse over at link it shows me localhost/ci/localhost/ci/assets/css/bootstrap.css link address. please
help me to solve this issue and help me to learn codeigniter.

Make sure you set the base_url right in application/config/config.php file:
$config['base_url'] = 'http://localhost/ci/';
And of course make sure to use the correct .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Related

trying to open a page using a link codeigniter php

I am trying to open a page on clicking on a link in my codeigniter application. This is the snippet of my attempt
at the controller level this is snippet
//load return policy
function returnpolicy()
{
$this->load->view("return_policy");
}
at the view level this is the snippet
<div class="toggle">
Return Policy
</div>
on clicking on the link it does not navigate to the page. please what am I missing. am new to codeigniter
You should understand the difference between site_url and base_url first
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
You can rewrite it as follows :
<?php echo site_url('controller/method');?>
In HMVC
<?php echo site_url('module/controller/method');?>
So in your case
Return Policy
you can use the URL helper this way to generate an <a> tag
anchor(uri segments, text, attributes)
So... to use it...
<?php echo anchor('gotopolicy/returnpolicy', 'Return Policy', 'class="toggle-title"') ?>
Possible issue could be of htaccess,
create a file .htaccess in your root CI folder
and add below contents to it
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Make $config['index_page'] = "";
try to remove class="toggle-title"
or try this
<div >
Return Policy
</div>

Codeigniter url set to another page

I am working with codeigniter but i dont know how to put link to another page. My controller filename is aboutus.php. I gave a link like
AboutUs
My base url is
$config['base_url'] = "http://localhost/project/";
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
But above url not working. I write a url directly in browser and hit like
http://localhost/project/index.php/aboutus then its working fine. how to give a url? i am confused.
base_url() will echo:
http://localhost/project
where as site_url() will echo:
http://localhost/project/index.php
You want to get to http://localhost/project/index.php/aboutus but with base_url() you're only getting to http://localhost/project/aboutus which is giving you the error.
You can do two things,
this:
AboutUs
which means adding the index.php before aboutus
or this:
AboutUs
which means changing base_url() to site_url().
Make sure that you are loading the helper in the controller:
$this->load->helper(url);
Or in application/config/autoload.php go to the line which says:
$autoload['helper'] = array();
and do this:
$autoload['helper'] = array('url');
and it will be included in every controller you now have.
If you have short tags enabled you can write your a tag like this:
About Us
or if you have the url helper you can write it like this:
echo anchor('aboutus', 'About Us', 'title="About Us"');
Try site_url() instead of base_url() so index.php will not be skipped
AboutUs
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
Also, make sure that url helper is loaded.
$this->load->helper('url'); //can also be done in config/autoload.php
Before using CI functions base_url() and site_url()
you need to load URL helper either in autoload.php or in controller itself.
If you have short tags enabled in PHP you can also write it link that:
Link Text
Use this .htaccess on your base folder
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If YOu still not get the result Please enable mod_rewrite in your apache server
your Project URL is look like you didn't enabled php short tags. Thats not a problem. just try this code:
About Us

Mod Rewrite Rule re-writing incorrectly

In my .htaccess file i have the following re-write rule
RewriteRule ^([a-z]*)$ work.php?album=$1 [L]
I am reading all directories in root directoy and creating links like
code in PHP
$dir=glob('images/works/*');
$dir_listing=array();
foreach($dir as $list)
{
if(is_dir($list))
$dir_listing[]= (basename($list));
}
foreach($dir_listing as $folders )
echo"<a href='$folders' ><img src='Default-thumbnail-folder.jpg' /> </a>";
the problem is i have three folders under works directory {alb,ban,bas}
the first and last {alb and bas} work properly for my redirect rule URL{siteName/works/bas}
but the second {ban} creates a URL like {siteName/works/ban/} Notice the extra "/" is creating the problem, it seems for some folder names it is creating the problem, so please help me in creating a proper redirect rule
i want a link like
<a href="alb" >About</a> // to redirect to work.php?content=about
I also notice that if URL in browser is like work/alb/ CSS is not applied
i tried
<link href="sample.css" rel="stylesheet" type="text/css" />
and
<link href="css/sample.css" rel="stylesheet" type="text/css" />
it does not work for URL like {works/about/} it works for {works/about}
Easy!
Try to change your rewriterule to:
RewriteRule ^([a-z]*)/?$ work.php?album=$1 [L]
So that the "/" at the end may or may not be present, in both cases it's ok.
to avoid rewriting css URI's use these rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
as for your other problem you can use a trim on the result url to make sure you never have a "/" at the end
trim($url,'/');

Invalid controller specified (css) in Zend framework

I am trying to apply css in the Zend Framework. My css file is in public/css/layout.css. First of all I added the following code lines in bootstrap.php:
protected function _initPlaceholders()
{
$this->bootstrap('View');
$view = $this->getResource('View');
$view->doctype('XHTML1_STRICT');
// Set the initial title and separator:
$view->headTitle('My Site')
->setSeparator(' :: ');
// Set the initial stylesheet:
$view->headLink()->prependStylesheet('../../css/layout.css');
}
Which simply specifies the title and stylesheet to be used. Then in layout.phtml I add the following code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
echo $this->headTitle();
echo $this->headScript();
// add a link to the site style sheet
echo '';
echo $this->headLink();
?>
</head>
Which simply adds link of the stylesheet specified in bootstrap.php.
But when I run the project then I am getting the form without css. Why is this? When I check the css with Firebug in a Mozilla browser it says:
<h3>Exception information:</h3>
<p>
<b>Message:</b> Invalid controller specified (css) </p>
<h3>Stack trace:</h3>
<h3>Request Parameters:</h3>
<pre>array (
'controller' => 'css',
'action' => 'layout.css',
'module' => 'default',
) </pre>
Please help me out to resolve this issue.
Try
$view->headLink()->prependStylesheet('/css/layout.css');
this should help.
Do you have this code on your /public/.htaccess file?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
If you don't have the first 4 lines, this file will redirect your request of "css/layout.css" to index.php and then, Zend will interpret it as a link for a controller -> action.
If you have this file, make sure that mod_rewrite is enabled on your server.
And you sould put your link as "akond" said.
Good luck =)
This helped me:
Zend_Controller tries to execute my stylesheets as controller
[update]
Ok, just added the following line to my .htaccess file and all works as expected now...
RewriteRule !.(js|ico|txt|gif|jpg|png|css|htc|swf|htm)$ index.php
but then I found this one:
http://devzone.zend.com/1651/managing-css-and-javascript-files-within-a-zend-framework-app/

PHP/Htaccess - problem with a loader

I have an htaccess file in the root that redirects every request of a page to a specific file:
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]
Now the redirect is easy in the loader.php
include($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['url']);
I just include the URL,admin/index.php for example.
If I leave the code as is the loader will include the file and print the correct HTML, but it will not load any CSS or JS scripts. If I put echo "test";
just before the include, the loader will load the CSS file. It's something that is killing me. Do I have to specify something in the HTTP header?
I already tried putting <base url="" /> in the header of index.php with no result,
but another strange thing is that with Chrom if I inspect the page and click on the link I'll see the right CSS.
Update 1
I printed the headers_list();. I noticed one thing - when I print an echo in headers_list, an array shows the content-type, so I tried to add it on my own with the header() function but with no result. Still working on it.
Update 2
I've noticed another thing; if I put a <style></style> tag with some CSS it will work fine, but if I use the <link /> tag it doesn't. This doesn't make any sense.
You have to send the correct MIME headers for css and javascript (and images etc.).
Easiest is to just let apache handle those requests. Put all the CSS, JS and images in a folder named 'assets' or something and change the htaccess to
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]

Categories