How do I create a Trax application?

From PHP on Trax

Jump to: navigation, search

Before you start make sure your Trax libraries are installed. see How do I install Trax?.

Contents

Step 1 - Create a skeleton application.

At the commandline cd to the place you want to create your application.

Current folder



trax .

or full path to where you want it



trax /path/to/application

Step 2 - Input settings for your application.

1. You have to let Trax know where your PEAR installation is so it can use PEAR::DB and PEAR:Mail_mime. From your application root edit the file config/environment.php and set where your php libs directory is at, the directory where your PEAR install is located. As the comments below state, you shouldn't need to set these but if Trax can't figure them out then you can uncomment and set them manually.



<?
# Trax should be able to figure the following 2 settings out
# automatically, but if you have trouble you can set them manually
# define("PHP_LIB_ROOT",    "/usr/local/lib/php");
# define("TRAX_ROOT",       dirname(dirname(__FILE__)));
?>

You may need to edit a property in config/environment.php to let Trax know if you are using a non-root url prefix. For example, if you created a Trax application under your webserver root, you may need to:



# change:
# Trax::$url_prefix = "~username";
# to:
Trax::$url_prefix = "trax/public";

Additionally, the public/.htaccess file may need to be modified. For example, if you created a Trax application under your webserver root, you may need to:



Change:
RewriteRule ^(.*)$ /dispatch.php?$1 [QSA,L]
To:
RewriteRule ^(.*)$ /trax/public/dispatch.php?$1 [QSA,L]

2. Only other thing to setup is if your going to use a database then put in the database settings. The config file for the database is database.ini in the config folder of your application root. Again open database.ini and change the apropriate settings. Depending on what mode Trax is in it will use the corresponding database settings. (This is optional. If you don't have a database then its not needed.)



[development]
  phptype = mysql
  database = mydatabase_development <= change
  hostspec = localhost
  username = root <= change
  password = ***** <= change
  persistent = true

[test]
  use = development  <= this can be used on any level to use other levels settings

[production]
  phptype = mysql
  database = database
  hostspec = localhost
  username = root
  password =
  persistent = true

Step 3 - Creating your application! (Models, Controllers, and Views)

Now that you have steps 1 and 2 done you should be able to go in your browser to your domain.com and see the default Trax Congratulations page. If that works then you are ready to start using Trax to build your application. In the trax/scripts folder there is a file named generate.php. That file is a commandline php script to generate Models, Controllers, and Views. If you type at the commandline ./generate.php controller or ./generate.php model it will show you helps on the correct syntax.

Creating a Model ( model names should always be singular ):



[root@bsd1 /home/john/trax/script]# ./generate.php model
Usage: ./generate.php model ModelName
Description:
        The model generator creates functions for a new model.
        The generator takes a model name as its argument.  The model name may be
        given in CamelCase or under_score and should not be suffixed with 'Model'.
        The generator creates a model class in app/models.
Example:
        ./script/generate.php model Account
        This will create an Account model:
                Model:      app/models/account.php

Creating a controller:



[root@bsd1 /home/john/trax/script]# ./generate.php controller
Usage: ./generate.php controller ControllerName [view1 view2 ...]

Description:
        The controller generator creates functions for a new controller and its views.

        The generator takes a controller name and a list of views as arguments.
        The controller name may be given in CamelCase or under_score and should
        not be suffixed with 'Controller'.  To create a controller within a
        module, specify the controller name as 'folder/controller'.
        The generator creates a controller class in app/controllers with view
        templates in app/views/controller_name.

Example:
        ./script/generate.php controller CreditCard open debit credit close

        Credit card controller with URLs like /credit_card/debit.
                Controller: app/controllers/credit_card_controller.php
                Views:      app/views/credit_card/debit.phtml [...]

Folders Example:
        ./script/generate.php controller 'admin/credit_card' suspend late_fee

        Credit card admin controller with URLs /admin/credit_card/suspend.
                Controller: app/controllers/admin/credit_card_controller.php
                Views:      app/views/admin/credit_card/suspend.phtml [...]

Notes

Logs

If your install didn't set the log files for Trax as writable or owned by the webserver then you will have to do it manually. First cd to the trax folder then do one of the following: 1. Chown the logs folder to the webserver user (www, apache, etc).



chown -R www logs

2. Chmod the logs folder so the world can write to it (777).



chmod -R 777 logs 

Routing

In Trax you can define what are called "routes". A route is a specific url that gets translated into a certain controller and action. The standard format of the url is domain.com/controller/action/id. Controller being the controller to load, action being the method in the controller to run, and id being the passed in id if any. You don't need to do anything here except if you want to define special cases where they type one url and it loads a different controller, An example is if you want Trax to handle the root URL meaning nothing after domain.com. This is done by the adding the following to config/routes.php



<?
$router
->connect"", array(":controller" => "home") );
?>

The above will route for the URL domain.com (the root of the domain). It will load the controller "home" and the default action "index".

Another example of routing is say I want a specific name to goto a certain product in my catalog, a shirt called cool shirt, but I don't want to have to type domain.com/catalog/show/25 and instead want to just be able to type domain.com/coolshirt. The below example shows how to do this.



<?
$router
->connect"coolshirt", array(":controller"=>"catalog",":action"=>"show",":id"=>"25") );
?>
Personal tools