Skip to content

Instantly share code, notes, and snippets.

@gravitano
Last active August 29, 2015 14:03
Show Gist options
  • Save gravitano/3d7bb622bc4efa5a8e68 to your computer and use it in GitHub Desktop.
Save gravitano/3d7bb622bc4efa5a8e68 to your computer and use it in GitHub Desktop.
Drop the static system of Laravel using this trait :D
<?php
trait LaravelTrait
{
/**
* The Laravel Aliases.
*
* @var array
*/
protected $aliases = array(
'app' => 'app',
'artisan' => 'artisan',
'auth' => 'auth',
'auth_reminder_repository' => 'auth.reminder.repository',
'blade' => 'blade.compiler',
'cache' => 'cache',
'cache_store' => 'cache.store',
'config' => 'config',
'cookie' => 'cookie',
'crypt' => 'encrypter',
'db' => 'db',
'events' => 'events',
'files' => 'files',
'form' => 'form',
'hash' => 'hash',
'html' => 'html',
'lang' => 'translator',
'translator' => 'translator',
'log' => 'log',
'mailer' => 'mailer',
'mail' => 'mailer',
'paginator' => 'paginator',
'auth_reminder' => 'auth.reminder',
'queue' => 'queue',
'redirect' => 'redirect',
'redis' => 'redis',
'request' => 'request',
'input' => 'request',
'router' => 'router',
'session' => 'session',
'session_store' => 'session.store',
'remote' => 'remote',
'url' => 'url',
'validator' => 'validator',
'view' => 'view',
);
/**
* Handle get property.
*
* @param $key
* @return mixed
*/
public function __get($key)
{
$app = app();
if($key == 'laravel' || $key == 'app')
{
return $app;
}
if(array_key_exists($key, $this->aliases))
{
return $app[$this->aliases[$key]];
}
return $this->$key;
}
}
@gravitano
Copy link
Author

Example Usage :

<?php

class HomeController extends BaseController
{
    use LaravelTrait;

    public function create()
    {
        return $this->view->make('users.create');
    }

    public function store()
    {
        $input = $this->input->all();

        $validation = $this->validator->make($input, User::$rules);

        if($validation->fails())
        {
            return $this->redirect->back()->withErrors($validation);
        } 

        User::create($input);

        return $this->redirect->back()->withFlashMessage('User created!');
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment