ZF2 installation on shared hosting server

2019-01-17 13:53发布

I am trying to install ZF2 on my shared hosting server. I followed these steps:

  1. Downloaded and extracted Zend Framework 2 on my computer.

  2. Created a directory on my "public_html" with the name of "zendPrj".

  3. Uploaded the "Zend" directory using Filezilla into the "zendPrj" folder.

  4. Created a "php.ini" with the following commands:

    include_path = ".:/usr/lib/php:/usr/local/lib/php:/home/public_html/mycpanalusername/zndPrj/Zend/library"
    allow_url_fopen = On
    allow_url_include = On
    

    and uploaded it into the "Zend" folder (which has got the "library", "bin", "resources" folders in it).

  5. Created a ".htaccess" file with the following instruction:

    SetEnv PHPRC /home/mycpanalusername/public_html/zendPrj/Zend/php.ini
    
  6. Created a testing file as follows:

    require_once 'Zend/Mail.php';
    $mail=new Zend_Mail();
    print_r($mail);
    echo 'it is working';
    

But I don't get any results. Instead, a blank page is shown. Where am I going wrong?

2条回答
Viruses.
2楼-- · 2019-01-17 14:13

OK, this is how I had setup ZF2 application in my shared hosting

folder structure for my application which should be copied to the sub domain folder (which is same as zf2 application structure)

ZF2 folder structure

Step 1: copy zf2 library (i.e ZEND folder inside zendframework download) to the root folder of your host i.e. after copy it looks like this(in my case) /home/username/zf2lib/ZEND (any location not accessible by public)

Step 2: edit .htaccess

#SetEnv ZF2_PATH /home/username/zf2lib
RewriteEngine On
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]

Step 3: edit init_autoloader.php to point to zf2 library, in this we have two option 1. if your host support SetEnv tag in .htaccess then simply uncomment the first line in the above .htaccess (mostly shared host don't provide these option, especially in my case) if the option 1 is not possible then you have to edit the init_autoloader.php file as below

enter image description here

Comment out from line 24 to 32, this code is responsible for various option to init the zend library, in our case we know where zf2 lib are loaded, so we are going to hard code the path, add line 33 and change username to your host user name.

Final step: copy all this content to your sub domain folder and access the site with subdomain.domain.com it all works well for me, hope it work for you too, good luck!

查看更多
闹够了就滚
3楼-- · 2019-01-17 14:17

If you're trying to just access one of the classes (i.e. Mail) then you should note that in ZF2 the library structure changed to follow PSR-0. So Zend/Mail.php doesn't exist and the class name is not Zend_Mail. To access the mail class you need to include Zend/Mail/Mail.php and the class name is just Mail.

If you're trying to use the full MVC stack, then you should do what Raj suggested.

Also, if you want to see what the error is, you might want to include this at the top of your file:

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
查看更多
登录 后发表回答