Getting started: A skeleton application

In order to build our application, we will start with theZendSkeletonApplication available on github. Use Composer to create a new project from scratch:

$ composer create-project -s dev zendframework/skeleton-application path/to/install

This will install an initial set of dependencies, including:

  • zend-component-installer, which helps automate injection of component configuration into your application.
  • zend-mvc, the kernel for MVC applications.

The default is to provide the minimum amount of dependencies necessary to run a zend-mvc application. However, you may have additional needs that you know at the outset, and, as such, the skeleton also ships with an installer plugin that will prompt you for a number of items.

First, it will prompt:

    Do you want a minimal install (no optional packages)? Y/n

Prompts and default values

All prompts emitted by the installer provide the list of options available, and will specify the default option via a capital letter. Default values are used if the user presses "Enter" with no value. In the previous example, "Y" is the default.

If you answer "Y", or press enter with no selection, the installer will not raise any additional prompts, and finish installing your application. If you anser "n", it will continue prompting you:

    Would you like to install the developer toolbar? y/N

The developer toolbar provides an in-browser toolbar with timing and profiling information, and can be useful when debugging an application. For the purposes of the tutorial, however, we will not be using it; hit either "Enter", or "n" followed by "Enter".

    Would you like to install caching support? y/N

We will not be demonstrating caching in this tutorial, so either hit "Enter", or "n" followed by "Enter".

    Would you like to install database support (installs zend-db)? y/N

We will be using zend-db extensively in this tutorial, so hit "y" followed by "Enter". You should see the following text appear:

    Will install zendframework/zend-db (^2.8.1)
When prompted to install as a module, select application.config.php or modules.config.php

The next prompt is:

    Would you like to install forms support (installs zend-form)? y/N

This tutorial also uses zend-form, so we will again select "y" to install this; doing so emits a similar message to that used for zend-db.

At this point, we can answer "n" to the remaining features:

    Would you like to install JSON de/serialization support? y/N
Would you like to install logging support? y/N
Would you like to install MVC-based console support? (We recommend migrating to zf-console, symfony/console, or Aura.CLI) y/N
Would you like to install i18n support? y/N
Would you like to install the official MVC plugins, including PRG support, identity, and flash messages? y/N
Would you like to use the PSR-7 middleware dispatcher? y/N
Would you like to install sessions support? y/N
Would you like to install MVC testing support? y/N
Would you like to install the zend-di integration for zend-servicemanager? y/N

At a certain point, you'll see the following text:

Updating root package
Running an update to install optional packages ... Updating application configuration... Please select which config file you wish to inject 'Zend\Db' into:
[0] Do not inject
[1] config/modules.config.php
Make your selection (default is 0):

We want to enable the various selections we made in the application. As such, we'll choose 1, which will then give us the following prompt:

  Remember this option for other packages of the same type? (y/N)

In our case, we can safely say "y", which will mean we will no longer be prompted for additional packages. (The only package in the default set of prompts that you may not want to enable by default is Zend\Test.)

Once the installation is done, the skeleton installer removes itself, and the new application is ready to start!

Downloading the skeleton

Another way to install the ZendSkeletonApplication is to use github to download a compressed archive. Go to https://github.com/zendframework/ZendSkeletonApplication, click the "Clone or download" button, and select "Download ZIP". This will download a file with a name like ZendSkeletonApplication-master.zip or similar.

Unzip this file into the directory where you keep all your vhosts and rename the resultant directory to zf2-tutorial.

ZendSkeletonApplication is set up to use Composer to resolve its dependencies. Run the following from within your new zf2-tutorial folder to install them:

$ composer self-update
$ composer install

This takes a while. You should see output like the following:

Installing dependencies from lock file
- Installing zendframework/zend-component-installer (0.2.0) ... Generating autoload files

At this point, you will be prompted to answer questions as noted above.

Alternately, if you do not have Composer installed, but do have either Vagrant or docker-compose available, you can run Composer via those:

# For Vagrant:
$ vagrant up
$ vagrant ssh -c 'composer install'
# For docker-compose:
$ docker-compose build
$ docker-compose run zf composer install

Timeouts

If you see this message:

[RuntimeException]
The process timed out.

then your connection was too slow to download the entire package in time, and composer timed out. To avoid this, instead of running:

$ composer install

run instead:

$ COMPOSER_PROCESS_TIMEOUT=5000 composer install

Windows users using WAMP

For windows users with wamp:

  1. Install composer for windows. Check composer is properly installed by running:
$ composer
  1. Install GitHub Desktop for windows. Check git is properly installed by running:
$ git
  1. Now install the skeleton using:
$ composer create-project -s dev zendframework/skeleton-application path/to/install

We can now move on to the web server setup.

Web Servers

In this tutorial, we will step you through four different ways to setup your web server:

  • Via the PHP built-in web server.
  • Via Vagrant.
  • Via docker-compose.
  • Using Apache.

Using the Built-in PHP web Server

You can use PHP's built-in web server when developing your application. To do this, start the server from the project's root directory:

$ php -S 0.0.0.0:8080 -t public/ public/index.php

This will make the website available on port 8080 on all network interfaces, using public/index.php to handle routing. This means the site is accessible viahttp://localhost:8080 or http://<your-local-IP>:8080.

If you’ve done it right, you should see the following.

To test that your routing is working, navigate to http://localhost:8080/1234, and you should see the following 404 page:

Development only

PHP's built-in web server should be used for development only.

Using Vagrant

Vagrant provides a way to describe and provision virtual machines, and is a common way to provide a coherent and consistent development environment for development teams. The skeleton application provides a Vagrantfile based on Ubuntu 14.04, and using the ondrej/php PPA to provide PHP 7.0. Start it up using:

$ vagrant up

Once it has been built and is running, you can also run composer from the virtual machine. As an example, the following will install dependencies:

$ vagrant ssh -c 'composer install'

while this will update them:

$ vagrant ssh -c 'composer update'

The image uses Apache 2.4, and maps the host port 8080 to port 80 on the virtual machine.

Using docker-compose

Docker containers wrap a piece of software and everything needed to run it, guaranteeing consistent operation regardless of the host environment; it is an alternative to virtual machines, as it runs as a layer on top of the host environment.

docker-compose is a tool for automating configuration of containers and composing dependencies between them, such as volume storage, networking, etc.

The skeleton application ships with a Dockerfile and configuration for docker-compose; we recommend using docker-compose, as it provides a foundation for mapping additional containers you might need as part of your application, including a database server, cache servers, and more. To build and start the image, use:

$ docker-compose up -d --build

After the first build, you can truncate this to:

$ docker-compose up -d

Once built, you can also run commands on the container. The docker-compose configuration initially only defines one container, with the environment name "zf"; use that to execute commands, such as updating dependencies via composer:

$ docker-compose run zf composer update

The configuration includes both PHP 7.0 and Apache 2.4, and maps the host port 8080 to port 80 of the container.

Using the Apache Web Server

We will not cover installing Apache, and will assume you already have it installed. We recommend installing Apache 2.4, and will only cover configuration for that version.

You now need to create an Apache virtual host for the application and edit your hosts file so that http://zf2-tutorial.localhost will serve index.php from thezf2-tutorial/public/ directory.

Setting up the virtual host is usually done within httpd.conf orextra/httpd-vhosts.conf. If you are using httpd-vhosts.conf, ensure that this file is included by your main httpd.conf file. Some Linux distributions (ex: Ubuntu) package Apache so that configuration files are stored in /etc/apache2and create one file per virtual host inside folder /etc/apache2/sites-enabled. In this case, you would place the virtual host block below into the file/etc/apache2/sites-enabled/zf2-tutorial.

Ensure that NameVirtualHost is defined and set to *:80 or similar, and then define a virtual host along these lines:

<VirtualHost *:80>
ServerName zf2-tutorial.localhost
DocumentRoot /path/to/zf2-tutorial/public
SetEnv APPLICATION_ENV "development"
<Directory /path/to/zf2-tutorial/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Make sure that you update your /etc/hosts orc:\windows\system32\drivers\etc\hosts file so that zf2-tutorial.localhost is mapped to 127.0.0.1. The website can then be accessed usinghttp://zf2-tutorial.localhost.

127.0.0.1 zf2-tutorial.localhost localhost

Restart Apache.

If you've done so correctly, you will get the same results as covered under the PHP built-in web server.

To test that your .htaccess file is working, navigate tohttp://zf2-tutorial.localhost/1234, and you should see the 404 page as noted earlier. If you see a standard Apache 404 error, then you need to fix your.htaccess usage before continuing.

If you're are using IIS with the URL Rewrite Module, import the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [NC,L]

You now have a working skeleton application and we can start adding the specifics for our application.

Error reporting

Optionally, when using Apache, you can use the APPLICATION_ENV setting in yourVirtualHost to let PHP output all its errors to the browser. This can be useful during the development of your application.

Edit zf2-tutorial/public/index.php directory and change it to the following:

<?php

use Zend\Mvc\Application;

/**
* Display all errors when APPLICATION_ENV is development.
*/
if ($_SERVER['APPLICATION_ENV'] === 'development') {
error_reporting(E_ALL);
ini_set("display_errors", 1);
} /**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__)); // Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server') {
$path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (__FILE__ !== $path && is_file($path)) {
return false;
}
unset($path);
} // Composer autoloading
include __DIR__ . '/../vendor/autoload.php'; if (! class_exists(Application::class)) {
throw new RuntimeException(
"Unable to load application.\n"
. "- Type `composer install` if you are developing locally.\n"
. "- Type `vagrant ssh -c 'composer install'` if you are using Vagrant.\n"
. "- Type `docker-compose run zf composer install` if you are using Docker.\n"
);
} // Retrieve configuration
$appConfig = require __DIR__ . '/../config/application.config.php';
if (file_exists(__DIR__ . '/../config/development.config.php')) {
$appConfig = ArrayUtils::merge($appConfig, require __DIR__ . '/../config/development.config.php');
} // Run the application!
Application::init($appConfig)->run();

Development mode

Before we begin, we're going to enable development mode for the application. The skeleton application provides two files that allow us to specify general development settings we want to use everywhere; these may include enabling modules for debugging, or enabling error display in our view scripts. These files are located at:

  • config/development.config.php.dist
  • config/autoload/development.local.php.dist

When we enable development mode, these files are copied to:

  • config/development.config.php
  • config/autoload/development.local.php

This allows them to be merged into our application. When we disable development mode, these two files that were created are then removed, leaving only the .dist versions. (The repository also contains rules to ignore the copies.)

Let's enable development mode now:

$ composer development-enable

Never enable development mode in production

You should never enable development mode in production, as the typical reason to enable it is to enable debugging! As noted, the artifacts generated by enabling development mode cannot be committed to your repository, so assuming you don't run the command in production, you should be safe.

You can test the status of development mode using:

$ composer development-status

And you can disable it using:

$ composer development-disable

 

Getting started: A skeleton application的更多相关文章

  1. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  2. 显示hello

    The modern user interface is constructed from visual objects of various sorts. Depending on the oper ...

  3. 高性能Web框架Zend Framework

    Zend Framework (ZF)是用 PHP 5.3及更高版本来开发 Web 程序和服务的开源框架.ZF用100% 面向对象编码实现. ZF的组件结构独一无二,每个组件几乎不依靠其他组件.这样的 ...

  4. Upgrading Applications

    Upgrading Applications If you have an existing Zend Framework v2 application, and want to update it ...

  5. Introducing the Blog Module

    Introducing the Blog Module Now that we know about the basics of the zend-mvc skeleton application, ...

  6. Using zend-navigation in your Album Module

    Using zend-navigation in your Album Module In this tutorial we will use the zend-navigation componen ...

  7. Setting up a database adapter

    Setting up a database adapter zend-db provides a general purpose database abstraction layer. At its ...

  8. Internationalization

    Internationalization If you are building a site for an international audience, you will likely want ...

  9. Advanced Configuration Tricks

    Advanced Configuration Tricks Configuration of zend-mvc applications happens in several steps: Initi ...

随机推荐

  1. oracle 有关大数据

    一. oracle大数据量分区后查询效率低下的一些建议: 1 对于当前表tm_bus_realtime_log.查看它的索引,只有一个(索引名:INDEX_BUS_REALTIME 字段名:UPLOA ...

  2. Tomcat 7 Connector 精读(2) CoyoteAdapter

    这个适配器类只讲2个方法,构造方法中我们看到一个适配器对象有自己关联的连接器类. 其中Service的重要任务就是讲客户端端请求交给容器. public void service(org.apache ...

  3. 5个最优秀的Java和C#代码转换工具

    http://www.codeceo.com/article/5-java-csharp-convert-tools.html 毋庸置疑,Java是一门最受欢迎而且使用最广泛的编程语言,目前有超过9百 ...

  4. leetcode@ [36/37] Valid Sudoku / Sudoku Solver

    https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...

  5. inter

    网卡不稳定的罪魁祸首   近期新上的DB SERVER服务器,在压测中发现网卡很不稳定,压力测试刚刚进行十几分钟后,服务器反应就变得非常慢,PING的时候经常丢包而且SSH连接也时断时 续.刚开始以为 ...

  6. CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )

    A,给一棵完全二叉树,第一个操作,给两个点,两点路径上的所有边权值都增加w,第二个操作,给两个点,求两点路径上的所有边权值和. 我看一眼题就觉得是树链剖分,而我又不会树链剖分,扔掉. 后来查了题解,首 ...

  7. 纯CSS完美实现垂直水平居中的6种方式

    前言 由于HTML语言的定位问题,在网页中实现居中也不是如word中那么简单,尤其在内容样式多变,内容宽高不定的情况下,要实现合理的居中也是颇考验工程师经验的.网上讲居中的文章很多,但是都不太完整,所 ...

  8. [iOS基础控件 - 6.2] LOL英雄列表 UITableView单项显示

    A.需求 1.使用只有一个section的TableView来显示LOL 的英雄列表 2.内容包括标题.副标题.图标 3.使用plain样式 4.使用MVC模式     heros.plist 文件结 ...

  9. jQuery语法总结及注意事项

    1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...

  10. Stage3D学习笔记(六):旋转动画效果

    我们这节在上一篇代码的基础上再进一步,让显示的纹理进行旋转. 实现旋转,只需要每帧修改_modelViewMatrix的旋转角度即可,我们需要一个变量来记录旋转: //旋转度数 private var ...