Skip to main content

Getting Started With Aura v2

·438 words·3 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

Yesterday aura framework v2 stable released.

Lots of complains about documentation or missing documentation. So this is a quick start. Probably a five minutes walk through. Learn and change to make it better.

Creating your project
#

Create the project using composer.

1
2
composer create-project aura/web-project quick-start
cd quick-start

The minimal framework don’t come with any sort of view integrated. Let us use aura/view, the two step templating with the help of foa/html-view-bundle.

1
composer require "foa/html-view-bundle:~2.0"

We will be keeping all the templates in templates folder where views in templates/views and layout in templates/layouts.

1
mkdir -p templates/{views,layouts}

Edit config/Common.php and define service for view.

1
2
3
4
5
<?php
public function define(Container $di)
{
    $di->set('view', $di->lazyNew('Aura\View\View'));
}

add a way to set the path to templates. Assuming you have templates folder in the root. There is no finder in aura/view to increase the performance of loading and rendering templates. For a quick hack let us iterate through the directory and set all the views and layouts to its registry.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
public function modify(Container $di)
{
    // more code
    $this->defineTemplates($di);
}

public function defineTemplates($di)
{
    $view = $di->get('view');
    $view_registry = $view->getViewRegistry();
    $view_directory = dirname(__DIR__) . '/templates/views/';
    $iterator = new \DirectoryIterator($view_directory);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $view_registry->set($fileinfo->getBasename('.php'), $fileinfo->getPathname());
        }
    }

    $layout_registry = $view->getLayoutRegistry();
    $layout_directory = dirname(__DIR__) . '/templates/layouts/';
    $iterator = new \DirectoryIterator($layout_directory);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $layout_registry->set($fileinfo->getBasename('.php'), $fileinfo->getPathname());
        }
    }
}

Edit modifyDispatcher method to

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
public function modifyWebDispatcher($di)
{
    $dispatcher = $di->get('aura/web-kernel:dispatcher');

    $view = $di->get('view');
    $response = $di->get('aura/web-kernel:response');
    $request = $di->get('aura/web-kernel:request');
    $dispatcher->setObject('hello', function () use ($view, $response, $request) {
        $name = $request->query->get('name', 'Aura');
        $view->setView('hello');
        $view->setLayout('default');
        $view->setData(array('name' => $name));
        $response->content->set($view->__invoke());
    });
}

Create your basic template templates/views/hello.php

1
2
3
<?php // templates/views/hello.php ?>
<?php $this->title()->set("Hello from aura"); ?>
<p>Hello <?= $this->name; ?></p>

and a very basic layout

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php // templates/layouts/default.php ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
  <head>
    <?php echo $this->title(); ?>
  </head>
  <body>
    <?php echo $this->getContent(); ?>
  </body>
</html>

Let us fire the php server

1
php -S localhost:8000 web/index.php

and point your browser to http://localhost:8000 .

Probably very simple way how to use aura as a micro framework!.

You can see the example over github.

What is next?
#

Read Aura Framework v2 : The missing Manual and report/contribute to the book.

Related

Installing an Aura Framework Project via Composer

·56 words·1 min
In this tutorial I am showing how to install aura framework project v2 via composer. composer create-project -s beta aura/framework-project path-to-project Aura.Framework_Project helps you to build web and cli applications. If you need only web based application then Aura.Web_Project is what you need. {% showterm b971330ea7fd28d22e2f3 %} If you need only cli, then Aura.Cli_Project helps you.

Using Aura.Input Forms Inside Slim Framework

·738 words·4 mins
Rob Allen wrote about Integrating ZF2 forms into Slim. I did write how you can use Aura.Input and Aura.Html to create standalone form for PHP. This time I felt I should write about integrating aura input inside Slim. Let us install a few dependencies aura/input for building the form and aura/html for the html helpers. You of-course can skip not to use aura/html and build your own helper. I also purposefully left not integrating the powerful Aura.Filter , but you are not limited to integrate any validator you love inside Aura.Input .

Aura Framework v2: The Missing Manual

·543 words·3 mins
Aura has an awesome collection of libraries for different purpose. It has components for authentication, cli, request and response, router, dependency injection container, dispatcher, html, view, event handlers, validation, extended pdo, query builders, sql schema, marshal, build and modify uri, http, internationalization, session, forms, includer. If you are new to aura, there is probably something you may want to figure out yourself. Some of the components have version 1 and version 2 releases. There is a question of which branch corresponds to which version. The v1 packages are in develop branch and v2 over develop-2 branch.