Wallogit.com
2017 © Pedro Peláez
Envision for Laravel 4
Envision is a framework extension for Laravel 4.2.
, (*1)
The goal of using Envision is speeding up development. It does a few things to help with that:, (*2)
Envision depends on the Ardent and the Presenter packages in your Laravel framework. So please add those packages as well.
Add shincode/envision, Ardent and Presenter to composer.json., (*3)
"require": {
"laravel/framework": "4.2.*",
"shincode/envision": "dev-master",
"laravelbook/ardent": "2.5.*",
"robclancy/presenter": "1.3.*"
}
Update with composer:, (*4)
composer update
Add the serviceproviders in app/config/app.php:, (*5)
'Shincode\Envision\EnvisionServiceProvider', 'Robbo\Presenter\PresenterServiceProvider',
Copy the config file:, (*6)
php artisan config:publish shincode/envision
In app/config/packages/shincode/envision/settings.php, set autoload and create to true to enable speed development, (*7)
'autoload' => true, 'create' => true,
Here's a mini tutorial. Let's create a table called phones for our database. Run the follow artisan command:, (*8)
php artisan migrate:make create_phones_table
Let's edit that file (keeping it simple), (*9)
public function up() {
Schema::create('phones', function(Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
Run the migration, (*10)
php artisan migrate
Now add this to app/routes.php, (*11)
Route::controller('phone', 'PhoneController');
Notice how you haven't created any files yet, but instantly refer to the PhoneController. Run the application.You'll see that a PhoneController.php has appeared in your app/controllers structure., (*12)
Let's open this controller and add a function, (*13)
public function getIndex() {
PhoneResource::insert(array('name' => 'Sunkia'));
PhoneResource::insert(array('name' => 'Nosy'));
PhoneResource::insert(array('name' => 'Namnung'));
}
See where it's going? You never actually made the classes, you're just writing what you want to do. First we created generic routing method. Now we populate the database a little bit so we have something to work with.
Since we created a controller route to 'phone', all we have to do now is to go URL /phone and the function will be executed.
Of course we normally use a seed for this, but this way you understand how easy it is to create new entries., (*14)
The next step is to display them. Let's see how that works. Change the getIndex to something else:, (*15)
public function getIndex() {
$phones = PhoneResource::all();
return View::make('phone')->with('phones', $phones);
}
Let's make a view file as well and save it as view/phone.blade.php, (*16)
@foreach($phones as $phone)
<h1>{{ $phone->name }}</h1>
@endforeach
Now open the URL to /phone and you'll notice it just works!, (*17)
By now you should see there are a few new files created. We've been using the Resource class, the Ardent class and the Presenter class., (*18)
Let's try using the Presenter class. There should be a new folder in your app folder called 'presenters'. Open PhonePresenter.php and add the following function, (*19)
public function presentFoo() {
return 'bar';
}
Now let's edit view/phone.blade.php to, (*20)
@foreach($phones as $phone)
<h1>{{ $phone->name }}</h1>
<span>{{ $phone->foo }}</span>
@endforeach
And there it is. Additional view logic right at your fingertips. For more information about the presenter, check here: https://github.com/robclancy/presenter, (*21)
Ardent example will be added later, for now you can read about ardent here: https://github.com/laravelbook/ardent, (*22)
For development, having Envision is really nice. But when deploying, it's highly recommended to turn off auto-generation.
In app/config/packages/shincode/envision/settings.php (if you have published the config) you can change the following:, (*23)
I recommend using environments. For example, I use the local environment to develop and have a copy of settings.php in app/config/packages/shincode/envision/local/settings.php. By default, I have both options on disabled by default in the package, so the package is essentially 'off' on install, for safety reasons., (*24)
All issues and pull requests should be filed on the shincode/envision repository., (*25)
Envision is open-sourced software licensed under the MIT license, (*26)