views:

447

answers:

3

All, My PHP Zend MVC Application structure is like this:

billingsystem
 -application
 -design
 -public
    --index.php
    --.htaccess
 -library
    -- Zend

whenever the application loads, it goes to index.php in public folder and it gets rerouted from there.. I want to make sure users to access the system by going http://billingsystem/ instead of going to http://billingsystem/public. Is this a Zend convention to keep the public folder. or can I get rid of it and move the files to the root directory? I tried doing this, but my application failed miserably, as it's not able to find the Zend library and load it's classes.. Some of my index.php code is as under:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

// Ensure include/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../include'),
    get_include_path(),
)));

// Ensure application/models is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../application/models'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'Zend/Loader.php';

//Autoload Zend Classes
Zend_Loader::loadClass('Zend_Loader_Autoloader');
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

Thanks

A: 

EDIT:

With your configuration you need to change to the following:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

After doing this you can move the contents of public up to your document root and it should work.

DO NOT FORGET/IGNORE THE PART ABOUT THE .htaccess files in the non public directories.

That said it really would be better to use the convention i described. Quite frankly, if your host doesnt allow you to create files outside the document root id switch hosts.


Typically the convention is that the public folder is the document root and all the other folders are outside the document root. So http://billingsystem.com should map to path/to/project/public via virtual host or default host configuration. Its not really a good idea to have everything in the document root. If you do put everything in the document root because you dont have access outside of it then you need to change your configuration:

in index.php

APPLICATION_PATH should be realpath(dirname(__FILE__).'/application');

Then youll want to make sure you drop a .htaccess in library,application, and tests that denies all web access.

prodigitalson
How can I make sure my application root "billingSystem" is the document root?
Vincent
Please see my edit above.
Vincent
+2  A: 

ZF assumes that you'll be setting the document root of your host to the public folder. This is essentially so you can have your application code outside the "public" space. If you're using an apache server, you'd modify the DocumentRoot directive in your httpd.conf (1.x) or apache2.conf (2.x) file. Your server itself can have a DocumentRoot, and you can also create VirtualHosts that each have DocumentRoots. Here's a decent tutorial on setting up Virtual hosts for your sites: http://apptools.com/phptools/virtualhost.php. There's also a good example in the Zend wiki: http://framework.zend.com/wiki/display/ZFDEV/Configuring%2BYour%2BURL%2BRewriter

Typeoneerror
If I change my virtualhost document root to /billingsystem/public, How will I access the content in application, include folders? I guess I will get access denied errors..
Vincent
@Vincent: No you wouldnt put any web accessible files in there. you would put all this in `public`. `application` shouldnt have anything that needs to be accessed by URL in it... nor should `includes`. Anything you are actually including with `include()` and `require` should always be done using filesystem paths. Essentially the only thing that should be in the public folder are images, css, and js scripts... pluss if you have stuff like an rss feed or something. but all php code except for `index.php` lives outside the document root.
prodigitalson
My public folder has only index.php and .htaccess.. What I am saying is: when a PHTML file in application/views/scripts/ folder makes a call to include a CSS in /design/css/struct.css , it can't find it, if I went through the above approach.. My question was how to configure these paths?
Vincent
@vincent: thats because your `design` folder should be inside public if it contains only public files... if it contains public and private files then you need to restructure this. typical structure is: `public/styles/*.css`, `public/scripts/*.js`, and `public/images/*.jpg|img|png`. if you need to add design in there then either symlink to the `design/$resourceType` in each public dir or reorganize so that design is a direct subfolder of the resource types.
prodigitalson
Yes, Vince. The idea is that anything public should be in the public folder. All your application logic goes outside the public folder so people can't (in theory) access those things. PHP can require and include files without issue as long as it's configured correctly. Move design inside the public folder and rewrite your paths.
Typeoneerror
A: 

Rob Allen explains how to achieve this here

http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/

Basically, you keep your project structure as it is.

You create an index.php file on the same level of your /public folder with the following

<?php 
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';

Also create a new .htaccess file on the same level of your /public folder with this

SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteRule .* index.php

Then you need to do some tricks in your application about how to reference static contents. That is specific to your own implementation, see the link for an implementation provided by Rob Allen.

Jim Li