Add CSS File To Layout Zend Framework - zend-framework

I'm trying to add a css file in my zend application layout.php the problem is it resides in
/application/media/css/style.css
when i do
<?php echo $this->headLink()->appendStylesheet('/media/css/style.css') ?>
it generates the path like
appname/public/media/css/style.css
where as i need to generate the path like
appname/application/media/css/style.css
how can i tell zend to look for the css file at a apecific location in layout.php

I have solved the problem. Always is simple. :D
I added
<link rel="stylesheet" href="css/site.css" type="text/css" media="screen, projection">
in head tag. And css folder is in public folder.
thanks
ohm

Everything in application directory is not accessible via your web server you would either have to move the file to the public directory or setup a symlink to it.

Just to complete all the answers, is not "correct" put your CSS, JS, fonts, and all other media in 'application' folder. The 'public' folder is there for that.
Then, to remove the '/public' from all your URLs, just make a simple change in your .htaccess
RewriteEngine On
RewriteBase / # you could put some subfolder, if you need
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
And in your index.php
<?php
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';
And then you could use this to append your styles
<?php echo $this->headLink()->appendStylesheet('/public/media/css/style.css') ?>
Hope it helps.

You should use this code:
<link rel="stylesheet" type="text/css" media="screen" href="<?=$this->baseUrl();?>/yourpathtocss" />

I think the correct way is to do this from layout.phtml as follows
<?php echo $this->headLink()->appendStylesheet('/css/site.css') ?>

here 'TestZend' is Project name
<?php echo $this->headLink()->appendStylesheet('/TestZend/public/css/bootstrap.css'); ?>

Just add this code snippet in the layout section to make sure the MEDIA folder is created inside yourProjectFoder/public/media/css/style.css
<head>
// some code here...
<?= $this->headLink()
//added from the new template
->prependStylesheet($this->basePath('media/css/style.css'))
?>
// some other code here
</head>

Related

How to remove "public" from url using routeing in zend framework

An one issue in my zend, i write rule in .htaccess to remove "public" from url as following,
------------------------------------------------------------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain-name.com/$ [OR]<br/>
ReWriteCond %{REQUEST_URI} !public/<br/>
ReWriteRule ^(.*)$ public/$1 [L]<br/>
------------------------------------------------------------------------
but there is ROUTE method in zend, i have use it for multiple language to set language code in url LIKE www.domain-name.com/en/ , using zend_controller_router_route_chain,
before this method implemented, my url is www.domain-name.com but
when i use this method in my zend project, may be it overwrites .htaccess rule of removing "public" from URL or something happen using same and "public" is displaying in url like www.domain-name.com/public.
so IS THERE ANY METHOD OR TRICK TO REMOVE PUBLIC FROM URL USING ANY METHOD OF ROUTE IN ZEND FRAMEWORK ???
Thanks,
MRJethva.
The following works for me using ZF2.2
Create index.php on ZF2 root directory and add the following content:
<?php
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';
Create .htaccess on ZF2 root directory and add the following content:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule .* index.php
Finally, add this conditional statement in the top of your layout.phtml file:
<?php
if (defined('RUNNING_FROM_ROOT')) {
$this->plugin('basePath')->setBasePath($this->basePath().'/public');
} ?>
Enjoy!
Reference: http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/
Yes, and you don't need Zend_Route. Delete the public folder and put the Zend Framework files from it (index.php, .htaccess, etc) in your root directory (e.g. htdocs). You can place the application folder and other Zend Framework files outside of your web root where they cannot be accessed over HTTP.
All you need to do is edit index.php and change the APPLICATION_PATH to the correct path. This way your Zend Application will run from your root directory and you won't need to use mod_rewrite to hide the public folder.
See the last part of this answer for a similar example.
zf2 and zf3 remove Public from URL
create index.php and .htaccess add file in root of project
add the line in index.php
include_once("public/index.php");
and in .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
after that go to following path
Module->application(module name)->view->layout->layout.phtml
change css and js path add public in path
before
->prependStylesheet($this->basePath('css/bootstrap.min.css'))
->prependFile($this->basePath('js/bootstrap.min.js'))
after
->prependStylesheet($this->basePath('public/css/bootstrap.min.css'))
->prependFile($this->basePath('public/js/bootstrap.min.js'))
also in image path

301 redirect all urls containing a certain phrase

I want to redirect all urls that contain "videoplayer.swf" in them. A load of pages got generated automatically with this and I want to 301 them all to the homepage.
<?php
if (preg_match ("videoplayer\.swf", $_SERVER['REQUEST_URI')) {
header (...);
exit;
}
?>
Or, if you're using apache, here's a .htaccess:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com/.*$ [NC]
RewriteRule ^.*videoplayer\.swf.*$ http://www\.example\.com/ [R,NC]
Based on checking the referrer, so you can have something like that viewable, if accessed from your site.

Running Zend Framework in a subfolder - Problems with default controller

I am starting a Zend application and I need to put it in a sub folder like this:
/subfolder
application
public
...
I did the following:
I set the base url in application.ini:
resources.frontController.baseUrl = "/subfolder"
I moved the .htaccess from the public folder directly into /subfolder, while changing the htaccess as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L]
That's all I did. And everything works as expected. I can add controller blog with action view and can access it by calling:
/subfolder/blog/view
the url view helper and stuff work right too. The only thing that does not work is the default controller. So when I enter:
/subolder/
I get 403 (forbidden). If I enter
/subfolder/index
...it works. What am I missing?!
Thanks in advance!
teebee
you do not need to make any change in your zf public folder, just create an .htaccess file into the "/subfolder" directory with the following lines
# Rewrite rules
RewriteEngine on
RewriteRule (.*) ./public/$1
Replace
RewriteRule ^.$ public/index.php [NC,L]
with
RewriteRule ^.$ http://addressto/index.php [R=302,NC]
or
RewriteRule ^.*$ /public/index.php [NC,L]
i'm not sure if putting ZF folders other than public into publicly accessible folder is right.It will impose your site on security flaws. You must put ZF'S public folder' contents into your server`s public folder and put other folders into a non-publicly accessible folder.If you need more that one instance of your ZF application you may use Zend module system.Please refer to zend online documentation for more details.

confusion of View Helpers

I'm on shared hosting where www only accessible by web server , so I change the structure recommended for the project based ZenFramework putting the library under the folder www :
www/
index.php
application/
Modules/
front/
admin/
Library/
the .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I tested locally under WampServer, everything works fine.
when I go online, I get errors on View Helpers :
Warning: include_once(): Unable to access /home/www/application/Modules/front/views/helpers/HeadMeta.php
there is confusion between ZendFramework views helpers and the view helpers that I create myself. and when I copy the content :
library \ Zend \ View \ Helper \
to
\ application \ modules \ front \ views \ helpers
everything works fine ! and it is the same concern for module admin/
I do not understand how why.
is what I have to keep the View helpers with my aides custom view ?
Thank you in advance for your answers.
Update:
I have not attached and any help create by myself . but I have to create action helpers, like this:
resources.frontController.moduleDirectory = APPLICATION_PATH "/Modules"
resources.frontController.defaultModule ="front"
resources.frontController.actionHelperPaths.Application_front_Controller_Helper = APPLICATION_PATH "/Modules/front/controllers/helpers/"
resources.modules[]=
Bootstrap module :
class front_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initHelper()
{
$this->bootstrap('frontController');
$navigation = Zend_Controller_Action_HelperBroker::getStaticHelper('NavigationPath');
Zend_Controller_Action_HelperBroker::addHelper($navigation);
}
}
Several thoughts/questions/comments:
It sure seems to be looking for the view helper within your Module folder. Are you attempting to call a custom HeadMeta helper developed by you, or are you are attempting to call the standard Zend_View_Helper_HeadMeta helper? Have you set any helper paths in either Bootstrap or application/config.ini?
Your current setup has a security issue: What if someone requests the url: http://exmaple.com/application/config.ini? I don't see any protection there. Note that you don't necessarily have to move all your ZF folders up in to the document root. Rather, you could push them down and place deny-all protection on that folder. See: Zend Framework on shared hosting. That blog post offers other approaches to ZF on shared hosting, as well.
Finally, when something works on Windows but doesn't work on Linux, there are several usual suspects to check.

Zend Framework in a subfolder

I have installed a Zend Framework application in a subfolder, something like this:
www.mydomain.com/temp/zend-application/
Now the problem is all stylesheets, javascript files and also all links use relative paths, such as:
/css/styles.css
/js/jquery.js
/controller/action
And these go to the main domain like this:
www.mydomain.com/css/styles.css
www.mydomain.com/js/jquery.js
www.mydomain.com/controller/action
So how to make it work in that subfolder?
My .htacces file looks like this now:
RewriteEngine On
# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
EDIT: I have solved the problem by using setBaseUrl() method of front controller. Now the problem is how to edit my .htaccess file.
The best solution would be to set a BaseUrl in your application. This should be detected automatically if you set a RewriteBase in your .htaccess. The router and other components have a knowledge of this setting and can generate appropriate links.
There's a view helper which makes linking to assets easier.
Alternatively you can fix those links with a tag in the document head.
<html>
<head>
<base href="http://www.mydomain.com/temp/zend-application/">
</head>
Further reading at http://www.w3schools.com/TAGS/tag_base.asp
I would definetly use baseUrl() view helper preppended to the CSS's and JS's paths.