PayPal - The safer, easier way to pay online!

PHP web projects - 5 minutes starter guide

Create a project

First, prepare Maven and follow the preparation instructions.

Next, type the following Maven goal in your command line and execute it:

 			
mvn archetype:generate \
-DarchetypeGroupId=org.phpmaven \
-DarchetypeArtifactId=php5-zend-archetype \
-DarchetypeVersion=2.0.2 \
-DgroupId=org.sample \
-DartifactId=my-app \
-Dversion=0.0.1-SNAPSHOT
			

After some time maven will ask to confirm the parameters. The result will look like this:

my-app 
	|-- pom.xml 
	`-- src 
		|-- main 
		|	|-- php 
		|	|	|-- application 
		|	|	|	|-- controllers 
		|	|	|	|	`-- ErrorController.php
		|	|	|	|	`-- IndexController.php
		|	|	|	|-- views
		|	|	|	|	`-- scripts
		|	|	|	|		|-- error
		|	|	|	|		|	`-- error.phtml
		|	|	|	|		`-- index
		|	|	|	|			`-- index.phtml
		|	|	|	`-- Bootstrap.php
		|	|	`-- public
		|	|		`-- index.php
		|	`-- resources
		|		|-- application
		|		|	`-- configs
		|		|		`-- application.ini
		|		|-- etc
		|		|	`-- logback.xml
		|		|	`-- phpunit.xml
		|		|-- public
		|		|	`-- .htaccess
		|		|-- webapp
		|		|	`-- WEB-INF
		|		|		`-- web.xml
		|		`-- .zfproject.xml
		|`-- test 
		|	`-- php 
		|		`-- ControllerTest.php
		`-- site
			|-- site.xml
			`-- apt
				`-- index.apt
			
  • src/main/php/lib - contains the project source code; as you see only *.php and *.phtml files of the zend directory layout.
  • src/main/php/resources - contains the additional resources; all non-php files from zend directory layout
  • src/test/php - contains the test source code
  • pom.xml The project's Project Object Model, or POM, looks like this:

The POM

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.phpmaven</groupId>
        <artifactId>php-parent-pom</artifactId>
        <version>2.0.2</version>
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <phpunit.version>3.6.10</phpunit.version>
        <zend.framework.version>1.11.11</zend.framework.version>
    </properties>

    <groupId>org.sample</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0</version>
    <packaging>php</packaging>

    <name>PHP 5 web Project</name>
    <description>Sample PHP 5 web project.</description>
    <url>http://www.php-maven.org</url>
    <inceptionYear>2008</inceptionYear>

    <build>
        <plugins>
            <!-- <plugin>
                <groupId>org.sample</groupId>
                <artifactId>own-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <extensions>true</extensions>
            </plugin> -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.0</version>
                <inherited>true</inherited>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.phpmaven</groupId>
                            <artifactId>maven-php-plugin</artifactId>
                            <version>${phpmaven.release.number}</version>
                            <reportSets>
                                <reportSet>
                                    <reports>
                                        <report>phpdocumentor</report>
                                        <report>phpunit-coverage</report>
                                        <report>phpunit</report>
                                    </reports>
                                </reportSet>
                            </reportSets>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
            
			<plugin>
			    <groupId>org.mortbay.jetty</groupId>
			    <artifactId>maven-jetty-plugin</artifactId>
			    <configuration>
			        <webXml>${project.basedir}/target/classes/webapp/WEB-INF/web.xml</webXml>
			        <webAppSourceDirectory>${project.basedir}/target/classes/public</webAppSourceDirectory>
			        <systemProperties>
			            <systemProperty>
			                <name>logback.configuration</name>
			                <value>${project.basedir}/target/classes/etc/logback.xml</value>
			            </systemProperty>
			            <systemProperty>
			                <name>phpIncludePath</name>
			                <value>${project.basedir}/target/php-deps/library;${project.basedir}/target/php-deps</value>
			            </systemProperty>
			        </systemProperties>
			    </configuration>
			</plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>de.phpunit</groupId>
            <artifactId>phpunit</artifactId>
            <version>${phpunit.version}</version>
            <type>phar</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.zend.framework</groupId>
            <artifactId>framework</artifactId>
            <version>${zend.framework.version}</version>
            <type>phar</type>
        </dependency>
    </dependencies>


</project>

What have I just done?

What have I just done?

You have just executed the Maven goal archetype:generate and passed various parameters to that goal. The prefix archetype is the plugin containing the goal. This goal created a default project structure for your PHP project.

Build the project

Switch to your sample project directory

cd my-app

and run:

mvn package

The command line will print out various actions, ending with the following:

 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Thu Jul 20 19:15:06 CDT 2008
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------			
			

Maven has created a phar package under the project's target folder.

What has Maven done?

"Maven for PHP" validates the PHP code with the php.exe, runs PHPUnit tests, and creates an assembly for the web project.

Let us start in webserver

Now run

mvn jetty:run
Maven will print various information and end with something like
jetty server started
Do not stop it here but start your web browser and navigate to
http://localhost:8080/my-app
You will see the zend frameworks welcome page (the index page). zend success

Documentation

Go to the "Documentation" section.

Credits

Special thanks to Owen Griffin.