-
Notifications
You must be signed in to change notification settings - Fork 0
Creating simple command example
dubpub edited this page Feb 17, 2015
·
7 revisions
Let's take a look at simple example how it works and say that we need a view extension, that would handle security and security-user functions from view. So we need to have access to laravel's Illuminate\Auth\Guard
and account model, defined in auth.php
config file. Since our command will delegates calls to account model we will use
LaravelCommode\Bladed\Commands\ADelegateBladedCommand
. Here's our class:
<?php
namespace MyApplication\BladedCommands;
use LaravelCommode\Bladed\Commands\ADelegateBladedCommand;
use LaravelCommode\Bladed\BladedManager\Interfaces\IBladedManager;
use Illuminate\Foundation\Application;
use Illuminate\Auth\Guard;
class Security extends ADelegateBladedCommand
{
/**
* @var Illuminate\Auth\Guard
*/
private $authGuard;
public function __construct(Application $app)
{
parent::__construct($app);
$this->authGuard = $app->make('auth');
}
public function guest()
{
return $this->authGuard->guest();
}
public function check()
{
return $this->authGuard->check();
}
public function viaRemember()
{
return $this->authGuard->viaRemember();
}
public function getDelegate()
{
return $this->authGuard->user();
}
public function __get($propertyName)
{
return $this->getDelegate()->{$propertyName};
}
}
Now let's create a service provider that would handle all our command registrations. For this example I'm going to use LaravelCommode\Common\GhostService\GhostService
service provider to make sure that LaravelCommode\Bladed\BladedServiceProvider
is loaded and registered:
<?php
namespace MyApplication\ServiceProviders;
use LaravelCommode\Common\GhostService\GhostService;
class BladedRegistryServiceProvider extends GhostService
{
protected function uses()
{
return ['LaravelCommode\Bladed\BladedServiceProvider'];
}
proteted function launching() { }
proteted function registering()
{
$this->with('commode.bladed', function (IBladedManager $manager) {
$manager->registerCommandNamespace(
'security' => 'MyApplication\BladedCommands\Security'
);
});
}
}
Now let's take a look at how it might work in our blade views:
@!?security.check() ?@>
<a href="/security/signin">Sign In</a>
<a href="/security/signup">Sign Up</a>
@?->
Welcome: @security.login @> <br />
Last seet: @security.lastSeen @> <br />
@?security.viaRemember() ?@>
<a class="forgetMe">Forget me after log out</a> <br />
@?>
@?security.hasRole('manager') ?@>
<a href="/manage">Management</a> <br />
@?>
@?>