views:

1755

answers:

3

Hello guys, am new to zend framework. Am having a little problem and a real hard time solving it myself.

If i access my zend application by going to http://localhost/zendapplicationfolder/contact it displays correctly with css applying correctly but if i access the zend application by going to http://localhost/zendapplicationfolder/contact/ (added a forward slash) , the page displays but css is not applied, but if i view the source, the css link is embedded normally between the head tags.

pls i need urgent help with this.

thanks.

+2  A: 

The dcode to include the CSS should be in a layout file, and should use the baseurl helper instead of manually writing the link to the css file.

Helper class:

class Zend_View_Helper_BaseUrl
{
    function baseUrl()  {
        return Zend_Controller_Front::getInstance()->getBaseUrl();
    }
}

Code in the layout:

<link rel="stylesheet" type="text/css" media="screen" href="<?=$this->baseUrl();?>/your/path/from base url/your.css" />
Itay Moav
i didnt code the css manually in the layout file.
War Coder
the code i used:<? $this->headlink()->appendStylesheet('css/screen.css', 'screen,projection'); ?><? $this->headlink()->appendStylesheet('css/ie.css', 'screen,projection', 'IE'); ?><? $this->headlink()->appendStylesheet('css/print.css', 'print'); ?><?= $this->headlink(); ?>
War Coder
Just add the baseUrl I gave you to the url you have:... $this->baseUrl().'/css/screen.css'......
Itay Moav
Tried it and i gotFatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'BaseUrl' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/;./views\helpers/;./application/views\helpers/' in
War Coder
Of course, You must create the class (see code above) as a view Helper, in the helper directory and name it according to what ZF expects.
Itay Moav
copied the Zend_View_Helper_BaseUrl and saved the file as Zend_View_Helper_BaseUrl.php in helpers folder under views folder, but i still got the error:Fatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'BaseUrl' was not found in the registry;
War Coder
This is as far as I can help on this platform, now you need a forum where you can past your configuration and check what you are doing wrong. Can you create any view Helper at all?
Itay Moav
OK - Save the file as BaseUrl.php and not the name you gave it.
Itay Moav
thanks for ur help. am new to Zend Framework and i guess i can't.i will very much appreciate if you can show me how to create a Helper probably i would get the rest from there. thanks.
War Coder
thanks,will give dat a try
War Coder
thanks for ur time. u really tried. but haven't gotten it yet.
War Coder
A: 

Without knowing your project layout I suspect that you could solve the problem by using /css/screen.css rather than css/screen.css That forward slash would give you an absolute path to your css directory rather than the relative one you are using.

For example, this works: <?php echo $this->headLink()->appendStylesheet('/css/main.css') ?> while this does what you describe: <?php echo $this->headLink()->appendStylesheet('css/main.css') ?> the only diff is the forward slash in front of css.

gaoshan88
thanks for the response. i will give you more info on my directory layout:webroot/ library/ application/ controllers/ models/ views/ scripts/ filters/ helpers/ layouts/ css/ index.php .htaccessthats the directory structure.
War Coder
sorry, my last comment didnt show the way i wanted it, but i can explain. my application, library, and css folder is in my webroot root folder. I tried what you suggested but its still not working.
War Coder
index.php is not in your webroot? It needs to be.
gaoshan88
Glad to be of assistance.
gaoshan88
A: 

I thought I had to change something to my codes when the stylesheets were added. Turns out that all I needed to change was to add the forward slash as pointed out by gaoshan88 (thank you!):

href="stylesheets/main3.css"
to
href="/stylesheets/main3.css"