2017 © Pedro Peláez
 

library envision

Envision for Laravel 4

image

shincode/envision

Envision for Laravel 4

  • Monday, March 26, 2018
  • by ShinCode
  • Repository
  • 1 Watchers
  • 3 Stars
  • 70 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 8 % Grown

The README.md

Envision

Envision is a framework extension for Laravel 4.2. ProjectStatus, (*1)

The goal of using Envision is speeding up development. It does a few things to help with that:, (*2)

  • Generate and autoload classes that Envision recognises on-the-fly.
  • Uses the Ardent package automatically.
  • Uses the Presenter package automatically.

Installation

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,

Example

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)

Deployment

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)

  • autoload = Automatically tries to find classes used by Envision. Can be left on but it has some impact on performance. For a final deployment, it's recommended to actually autoload all the files and create aliases in app.php.
  • create = Automatically generate classes when not found. Highly recommended to disable when deploying for obvious reasons.
  • log = Not doing anything at the moment.

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)

Contributing To Envision

All issues and pull requests should be filed on the shincode/envision repository., (*25)

License

Envision is open-sourced software licensed under the MIT license, (*26)

The Versions

26/03 2018

dev-master

9999999-dev

Envision for Laravel 4

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Shin Ho