4. Creating Your First Agavi Application

Once you have everything set up, create a new directory for your application and, in there, run

agavi project

Follow the on-screen instructions. In case of doubt, confirm the default values given by the wizard.

Warning

Agavi sets the permissions on yourproject/app/cache to "a+rwx", better known as "0777", which means the cache directory and all files in there will be read- and writeable by everyone. Remember that the compiled configs will, for example, still contain your database credentials in clear text. It might be a good idea to chmod the directory to the minimum required permissions so the folder is readable only by PHP or the Web Server. You have been warned.

And that's it! You're done! Fire up your browser and point it to the pub directory of your application. You will see a nice welcome page that tells you everyting worked fine. This note will stay there until you remove the corresponding routing rule from app/config/routing.xml. While you're at it, you can also throw away the two .png files in pub/ that belong to the welcome page, and, of course, the WelcomeToAgaviAction.class.php, WelcomeToAgaviSuccessView.class.php and WelcomeToAgaviSuccess.php template. You're now good to go and may add your own Actions. But before that, reload your browser - a blank page appears! Time for the infamous "Hello, World" example - open up the default action's template (remember, you entered the default action's name during agavi project, but it usually is IndexSuccess.php in the default module's templates directory) and type some text. Back to the browser (you did remember to save, right?), reload, and there it is, the text you entered! Well done, that was the first step with Agavi, and the next ones will be just as exciting.

If you want to add more Modules, Actions or Models to your application, you can use the agavi script again:

agavi module

agavi action

agavi model

The script uses code templates to generate all the files. You can use your custom code templates, too. Just put them whereever you like (look at src/buildtools to learn what templates there are and what the structure and file names look like). Then, create a new file called build.properties and add one line to it:

templates.dir = /my/project/path/mycodetemplates

Of course, you don't have to provide custom versions of all code templates. Agavi will fall back to the bundled templates if it couldn't find a specific one in the path you specified.

Tip

You will often want the code templates dir to sit inside your project directory. In order to avoid an absolute path in the setting, you can use ${project.dir} to refer to your project directory.

Also, you can have per-module code templates. Add a modules directory to your code templates folder, and then add a new folder with the same name as the module. Now you can add a code template in there, and it will only be used when generating something for that specific module.

In addition to the templates directory, you can specify the extension used for the page template files (like IndexSuccess.php). The default extension is .php. The extension is applied both to the code template file name and to the name of the template that is written. Example:

template.extension = .tpl

If you create an action called "Index" and a view called "Success", the command line script will copy Template.tpl.tmpl to IndexSuccess.tpl for you.

In case you're migrating from an Agavi 0.10 project where the app dir name is still webapp, or you simply don't want that directory in your project to be called app, you can customize this, too:

app.dirname = webapp