Getting Started


Pragwork is a pragmatic PHP web framework. It provides a structure and a set of libraries for web applications written in the PHP programming language. Therefore, a basic knowledge of PHP is a prerequisite for it. To find out more about PHP check some books or tutorials on the Internet. You should also have some skills in SQL database servers and front-end developing (HTML, CSS), since web applications usually deal with data stored in databases and produce output full of HTML/CSS code.

PHP is the language designed for the web development in the first place. Probably, if you know it a bit, you have programmed web applications already. A web application differs from the static HTML page in that, it interacts with its clients. It is a program on the server side using a common web protocol. Actually, it is a server script driven by HTTP requests, e.g., a simple page displaying server time, a blog, a bank account manager at your bank site, or huge systems like Twitter.

The application in Pragwork is an implementation of the MVC design pattern. Pragwork organizes the web application into separated regions of responsibility. According to the pattern name, M stands for the Model, V for the View, and C for the Controller part. Technically, models, views, and controllers are small entities concerned in appropriate areas. Here in Pragwork, models and controllers are classes, and views are template files rendered by controllers. Models usually are subclasses of the ActiveRecord\Model class and controllers are children of the Application\Controller class.

What is interesting, controllers are the only objects required by an application to run. Models and views are almost always used by an average application but in fact they are not necessary. After all, the application neither has to have an attached database nor it has to render a sophisticated view.

Controller methods responsible for a direct response to an HTTP request are called actions. If a method is referred as an action it always means the particular method connected with a concrete routing scheme, run as a reply to the HTTP request. Together with actions controller can have plenty of various methods, both user-defined and inherited.

Quick Setup

The Pragwork requires PHP and the Prag tool requires PHP-CLI to run. You may either download PHP directly from the PHP site or download the whole environment for PHP applications, that is, a web server with a PHP installation and a database at hand. There are available such packages already, e.g., WAMP, XAMPP, or MAMP. If you are just starting the PHP development you can find the detailed installation instruction here:

You are ready to setup the Prag tool. Download it directly from the project page. The Prag is a single script written in PHP, however it is named prag (without the '.php' extension). Unzip it and move to your favorite place, for example:

$ unzip
$ mkdir ~/prag
$ mv prag ~/prag/

Verify that everything is OK and Prag is working correctly.

$ cd prag
$ php prag
PRAG 1.1.0
The Console Tool for Pragwork 1.1.0
Copyright (c) 2009-2011 Szymon Wrozynski
Licensed under the MIT License

Type "php prag help" for more info.

It is done and you are ready to create your first web application in Pragwork.

The Prag and the First Work

The Prag has several commands to generate code for models, controllers, and views. Some of them can be abbreviated to just one letter, e.g., controller to c, model to m, etc. The full list of commands along with examples are available by the command help (h). Type php prag help if you want to see it or check documentation.

Now we are about to create the first project. The command used to generate a new project is work so it is a bit word-play ;). Change the directory to you favorite one and type:

$ php prag work test

After that you should see the generator output listing all created files:

PRAG 1.1.0
The Console Tool for Pragwork 1.1.0
Copyright (c) 2009-2011 Szymon Wrozynski
Licensed under the MIT License

Generating new project structure (test)...

Writing test/modules/activerecord.php
Writing test/modules/ActiveRecord/CallBack.php
Writing test/modules/ActiveRecord/Column.php
Writing test/modules/ActiveRecord/Configuration.php
Writing test/modules/ActiveRecord/Connection.php
Writing test/modules/ActiveRecord/DateTime.php
Writing test/modules/ActiveRecord/ConnectionManager.php
Writing test/modules/ActiveRecord/ActiveRecordException.php
Writing test/modules/ActiveRecord/RecordNotFound.php
Writing test/modules/ActiveRecord/DatabaseException.php
Writing test/modules/ActiveRecord/ModelException.php
Writing test/modules/ActiveRecord/ExpressionsException.php
Writing test/modules/ActiveRecord/UndefinedPropertyException.php
Writing test/modules/ActiveRecord/ReadOnlyException.php
Writing test/modules/ActiveRecord/ValidationsArgumentError.php
Writing test/modules/ActiveRecord/RelationshipException.php
Writing test/modules/ActiveRecord/HasManyThroughAssociationException.php
Writing test/modules/ActiveRecord/Expressions.php
Writing test/modules/ActiveRecord/Inflector.php
Writing test/modules/ActiveRecord/StandardInflector.php
Writing test/modules/ActiveRecord/Model.php
Writing test/modules/ActiveRecord/Reflections.php
Writing test/modules/ActiveRecord/InterfaceRelationship.php
Writing test/modules/ActiveRecord/AbstractRelationship.php
Writing test/modules/ActiveRecord/HasMany.php
Writing test/modules/ActiveRecord/HasOne.php
Writing test/modules/ActiveRecord/HasAndBelongsToMany.php
Writing test/modules/ActiveRecord/BelongsTo.php
Writing test/modules/ActiveRecord/Serialization.php
Writing test/modules/ActiveRecord/ArraySerializer.php
Writing test/modules/ActiveRecord/JsonSerializer.php
Writing test/modules/ActiveRecord/XmlSerializer.php
Writing test/modules/ActiveRecord/CsvSerializer.php
Writing test/modules/ActiveRecord/Singleton.php
Writing test/modules/ActiveRecord/SQLBuilder.php
Writing test/modules/ActiveRecord/Table.php
Writing test/modules/ActiveRecord/Utils.php
Writing test/modules/ActiveRecord/Memcache.php
Writing test/modules/ActiveRecord/Cache.php
Writing test/modules/ActiveRecord/Validations.php
Writing test/modules/ActiveRecord/Errors.php
Writing test/modules/ActiveRecord/MysqlAdapter.php
Writing test/modules/ActiveRecord/TablelessModel.php
Writing test/modules/ActiveRecord/OciAdapter.php
Writing test/modules/ActiveRecord/PgsqlAdapter.php
Writing test/modules/ActiveRecord/SqliteAdapter.php
Writing test/modules/application.php
Writing test/modules/Application/ModuleConfiguration.php
Writing test/modules/Application/Configuration.php
Writing test/modules/Application/Response.php
Writing test/modules/Application/StopException.php
Writing test/modules/Application/Singleton.php
Writing test/modules/Application/Store.php
Writing test/modules/Application/Flash.php
Writing test/modules/Application/Parameters.php
Writing test/modules/Application/Cookies.php
Writing test/modules/Application/Request.php
Writing test/modules/Application/Session.php
Writing test/modules/Application/Controller.php
Writing test/modules/image.php
Writing test/modules/mailer.php
Writing test/modules/paginate.php
Writing test/modules/tags.php
Writing test/modules/Tags/Form.php
Writing test/modules/markdown.php
Writing test/modules/test.php
Writing test/modules/Test/TestRequest.php
Writing test/modules/Test/ControllerTestCase.php
Writing test/modules/Test/Storage.php
Writing test/modules/Test/TestResponse.php
Writing test/modules/Test/UnitTestCase.php
Writing test/modules/Test/TestSuite.php
Writing test/modules/Test/TestCase.php
Writing test/modules/Test/TestInvoker.php
Writing test/modules/Test/MethodInvoker.php
Writing test/modules/Test/Reporter.php
Writing test/modules/Test/Mock.php
Writing test/modules/Test/MockSpecification.php
Writing test/modules/Test/MockMethodSpecification.php
Writing test/modules/Test/AssertionFailed.php
Writing test/modules/Test/ConsoleReporter.php
Writing test/modules/Test/Application/Singleton.php
Writing test/modules/Test/Application/Session.php
Writing test/modules/Test/Application/Controller.php
Writing test/app/Controllers/ApplicationController.php
Writing test/config/routes.php
Writing test/config/application.php
Writing test/config/activerecord.php
Writing test/sql/schema.sql
Writing test/sql/data.sql
Writing test/app/helpers/ApplicationHelper.php
Writing test/errors/403.php
Writing test/errors/404.php
Writing test/errors/405.php
Writing test/errors/500.php
Writing test/public/.htaccess
Writing test/public/robots.txt
Writing test/public/index.html
Writing test/public/index.php
Writing test/public/stylesheets/scaffold.css
Writing test/public/favicon.ico
Writing test/public/images/pragwork.png
Writing test/public/images/pragwork_small.png
Writing test/test/fixtures/default_test_fixture.php

Good! We have the new project named Test. Now you need to set the web server to point to the 'test/public' directory (see how in MAMP, WAMP, XAMPP). If everything is set up correctly and the web server is turned on you will see the initial page of the newly created project after typing http://localhost/ in the web browser.

In the next chapter you will get more details about the project structure shown above.