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/
Related
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]
I want to change my address bar via htaccess.
My link is: http://englishforyou.ir/index.php?content=about
And I want to change it to http://englishforyou.ir/content/about.My htaccess code is:
RewriteEngine on <br>
RewriteRule ^content/([A-Za-z0-9-]+)/?$ index.php?content=$1 [NC]
But it doesn't work. You can test it here: englishforyou.ir
My index file is:
<?php
require_once 'includes/function.php';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div>
test
About me
</div>
<?php loadContent('content', 'mainpage'); ?>
</body>
</html>
My function file is:
function loadContent($where, $default='') {
// Get the content from the url
// Sanitize it for security reasons
$content = filter_input(INPUT_GET, $where, FILTER_SANITIZE_STRING);
$default = filter_var($default, FILTER_SANITIZE_STRING);
// If there wasn't anything on the url, then use the default
$content = (empty($content)) ? $default : $content;
// If you found have content, then get it and pass it back
if ($content) {
// sanitize the data to prevent hacking.
$html = include $content.'.php';
return $html;
}
}
And my about me file is:
<div>
<h4>
This page is about me.
home
</h4>
</div>
Your .htaccess file is right and it is working.
You might have to refresh your browser without cache, or wait for it to refresh by itself.
If you are using Firefox or Chrome, use Ctrl+F5 to refresh overriding the cache.
Note: You just have to use the url http://englishforyou.ir/content/about instead of http://englishforyou.ir/index.php?content=about
EDIT
Add this line to your .htaccess to redirect from http://englishforyou.ir/index.php?content=about to http://englishforyou.ir/content/about
RewriteRule ^index\.php\?content\=(.*)$ /content/$1 [R=301,L]
Try something like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^system/([^/]*)\.html$ system.php?route=$1
This translates the url http://www.domain.com/system/home.html to http://www.domain.com/system.php?route=home
And make sure you are allowed to use .htaccess
if request to my site goes like:
my1stdomain.com/request-1/
my1stdomain.com/any-another-request/
it should open web-pages:
my2nddomain.com/folder/request-1.php
my2nddomain.com/folder/any-another-request.php
I put php file in the web-root of my1stdomain.com with name referer.php
and it looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /$ [OR]
RewriteCond %{REQUEST_FILENAME} (html|php)$
RewriteCond /home3/juraj1/public_html/referer.php -f
RewriteRule ^.*$ /referer.php [L]
</IfModule>
and referer.php looks like:
<?
$login=preg_replace('/\//i','',$_SERVER['REQUEST_URI']);
$html=file_get_contents('http://my2nddomain.com/folder/'.$login.'.html');
echo $html;
?>
but it returns only html content without css styles and images
Can you please help me with it?
I assume the HTML files on my2nddomain.com link to the css files/images in a relative way, like <img src="images/picture.jpg" />. But since this image does not exist on my1stdomain.com, the browser doesn't find it.
Solution 1:
Link to all images/css files with an absolute path, like <img src="http://my2nddomain.com/images/picture.jpg" />
Solution 2
Add the <base> tag to the HTML head of all the files on my2nddomain.com like this: <base href="http://my2nddomain.com" />. This can be achieved by changing the HTML on the fly like $html = str_replace('</head>','<base href="http://my2nddomain.com" /></head>',$html);
There are also other solution with rewriting every request to a css file/image on your 1st server to the corresponding file of the 2nd server, but this will be more complicated.
I have this in my .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.php
and this is how i work with the parameters :
<?php
$request = $_SERVER['REQUEST_URI'];
$request = substr($request,1);
$params = explode('/', $request);
$safe_pages = array('page1','page2','page3');
if(in_array($params[0],$safe_pages))
{
include($params[0].'.php');
}else
{
include('404.php');
}
?>
Let's say my site is : www.site.com
www.site.com/page -> works perfectly
www.site.com/page/parameter -> doesn't load any css or images because it looks for them in root/page instead of root/
I tried to use absolute links for my css and images but it still didn't work, so i guess the problem is in the .htaccess or i'm missing something.
The problem lies not within htaccess rewrite rules, but in your html - yuor links to static content (like css, images or scripts) should start with slash to be ralative to the domain. So instead of
<link href="css/style.css" />
<script src="js/script.js"><script>
Use
<link href="/css/style.css" />
<script src="/js/script.js"><script>
Other way is to use base tag but it can break named anchors on your page
Use
<base href="http://absolute_path_here/">
in your HTML before the closing </head>.
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]