ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
§£§Ó§Ö§Õ§Ö§ß§Ú§Ö §Ó Laravel 5
§³§Ö§â§Ó§Ú§ã-§Ü§à§ß§ä§Ö§Û§ß§Ö§â §Ó Laravel
§³§Ö§â§Ó§Ú§ã-§Ü§à§ß§ä§Ö§Û§ß§Ö§â
§ã§â§Ö§Õ§ã§ä§Ó§à §Õ§Ý§ñ §å§á§â§Ñ§Ó§Ý§Ö§ß§Ú§ñ
§Ù§Ñ§Ó§Ú§ã§Ú§Þ§à§ã§ä§ñ§Þ§Ú §Ü§Ý§Ñ§ã§ã§à§Ó
§Ú
§Ó§ß§Ö§Õ§â§Ö§ß§Ú§ñ §Ù§Ñ§Ó§Ú§ã§Ú§Þ§à§ã§ä§Ö§Û.
§£§ß§Ö§Õ§â§Ö§ß§Ú§Ö §Ù§Ñ§Ó§Ú§ã§Ú§Þ§à§ã§ä§Ö§Û (DI)
§Ü§à§ß§ã§ä§â§å§Ü§ä§à§â §Ú§Ý§Ú §Þ§Ö§ä§à§Õ-§ã§Ö§ä§ä§Ö§â
namespace AppJobs;
class PurchasePodcast implements SelfHandling
{
protected $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
§ã§Ö§â§Ó§Ú§ã §Ó§ß§Ö§Õ§â§×§ß
§Þ§à§Ø§Ö§Þ §Ý§Ö§Ô§Ü§à §á§à§Õ§Þ§Ö§ß§Ú§ä§î §Ö§Ô§à §ã §Õ§â§å§Ô§à§Û §â§Ö§Ñ§Ý§Ú§Ù§Ñ§è§Ú§Ö§Û
§®§à§Ø§ß§à §Ý§Ö§Ô§Ü§à §ã§à§Ù§Õ§Ñ§ä§î ?mock? §Ú§Ý§Ú §æ§Ú§Ü§ä§Ú§Ó§ß§å§ð
§â§Ö§Ñ§Ý§Ú§Ù§Ñ§è§Ú§ð §á§â§Ú §ä§Ö§ã§ä§Ú§â§à§Ó§Ñ§ß§Ú§Ú §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ
§³§Ó§ñ§Ù§í§Ó§Ñ§ß§Ú§Ö
§£§ã§Ö §á§â§Ú§Ó§ñ§Ù§Ü§Ú §ã§Ö§â§Ó§Ú§ã-§Ü§à§ß§ä§Ö§Û§ß§Ö§â§à§Ó §Ò§å§Õ§å§ä
§Ù§Ñ§â§Ö§Ô§Ú§ã§ä§â§Ú§â§à§Ó§Ñ§ß§í §Ó §ã§Ö§â§Ó§Ú§ã-§á§â§à§Ó§Ñ§Û§Õ§Ö§â§Ñ§ç
§³§Ö§â§Ó§Ú§ã-§á§â§à§Ó§Ñ§Û§Õ§Ö§â§í - §à§ã§ß§à§Ó§Ñ ?§á§Ö§â§Ó§à§ß§Ñ§é§Ñ§Ý§î§ß§à§Û
§Ù§Ñ§Ô§â§å§Ù§Ü§Ú? Laravel
§±§â§Ú§Ý§à§Ø§Ö§ß§Ú§Ö §Ú §Ó§ã§Ö §Ò§Ñ§Ù§à§Ó§í§Ö §ã§Ö§â§Ó§Ú§ã§í §Ù§Ñ§Ô§â§å§Ø§Ñ§ð§ä§ã§ñ
§é§Ö§â§Ö§Ù §ã§Ö§â§Ó§Ú§ã-§á§â§à§Ó§Ñ§Û§Õ§Ö§â§í
?§±§Ö§â§Ó§à§ß§Ñ§é§Ñ§Ý§î§ß§Ñ§ñ §Ù§Ñ§Ô§â§å§Ù§Ü§Ñ?
§â§Ö§Ô§Ú§ã§ä§â§Ñ§è§Ú§ñ §ä§Ñ§Ü§Ú§ç §Ó§Ö§ë§Ö§Û §Ü§Ñ§Ü:
¡ñ §á§â§Ú§Ó§ñ§Ù§Ü§Ú §ã§Ö§â§Ó§Ú§ã-§Ü§à§ß§ä§Ö§Û§ß§Ö§â§Ñ
¡ñ §ã§Ý§å§ê§Ñ§ä§Ö§Ý§Ú §ã§à§Ò§í§ä§Ú§Û (event listener)
¡ñ §á§à§ã§â§Ö§Õ§ß§Ú§Ü§Ú (middleware)
¡ñ §Þ§Ñ§â§ê§â§å§ä§í (routes)
§³§Ö§â§Ó§Ú§ã-§á§â§à§Ó§Ñ§Û§Õ§Ö§â§í
§è§Ö§ß§ä§â§Ñ§Ý§î§ß§à§Ö §Þ§Ö§ã§ä§à §Õ§Ý§ñ §ß§Ñ§ã§ä§â§à§Û§Ü§Ú §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ
§¦§ã§Ý§Ú §à§ä§Ü§â§í§ä§î
config/app.php
§á§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§Þ§í§Û §ã Laravel
§ä§à §å§Ó§Ú§Õ§Ú§Þ¡­ (deferred)
'providers' => [
/*
* Laravel Framework Service Providers...
*/
IlluminateAuthAuthServiceProvider::class,
IlluminateBroadcastingBroadcastServiceProvider::class,
IlluminateBusBusServiceProvider::class,
IlluminateCacheCacheServiceProvider::class,
IlluminateFoundationProvidersConsoleSupportServiceProvider::class,
§¦§ã§Ý§Ú §Ü§Ý§Ñ§ã§ã§í §ß§Ö §Ù§Ñ§Ó§Ú§ã§ñ§ä §à§ä §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§à§Ó
§ß§Ö §ß§Ñ§Õ§à §ã§Ó§ñ§Ù§í§Ó§Ñ§ä§î §Ú§ç §Ó §Ü§à§ß§ä§Ö§Û§ß§Ö§â§Ö
§¯§Ö §ß§å§Ø§ß§à §à§Ò§ì§ñ§ã§ß§ñ§ä§î §Ü§à§ß§ä§Ö§Û§ß§Ö§â§å, §Ü§Ñ§Ü §ã§à§Ù§Õ§Ñ§Ó§Ñ§ä§î §ï§ä§Ú §à§Ò§ì§Ö§Ü§ä§í
§³§Ö§â§Ó§Ú§ã§í §à§ä§â§Ñ§Ø§Ö§ß§Ú§ñ PHP (Reflection)
§£ §ã§Ö§â§Ó§Ú§ã-§á§â§à§Ó§Ñ§Û§Õ§Ö§â§Ö §Ó§ã§Ö§Ô§Õ§Ñ §Ö§ã§ä§î §Õ§à§ã§ä§å§á §Ü §Ü§à§ß§ä§Ö§Û§ß§Ö§â§å §é§Ö§â§Ö§Ù
§á§Ö§â§Ö§Þ§Ö§ß§ß§å§ð §ï§Ü§Ù§Ö§Þ§á§Ý§ñ§â§Ñ $this->app
§±§â§Ú§Ó§ñ§Ù§Ü§Ñ §Þ§Ö§ä§à§Õ§à§Þ bind()
§±§Ö§â§Ó§í§Û §Ñ§â§Ô§å§Þ§Ö§ß§ä §Ú§Þ§ñ §Ü§Ý§Ñ§ã§ã§Ñ §Ú§Ý§Ú §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ
§£§ä§à§â§à§Û §Ù§Ñ§Þ§í§Ü§Ñ§ß§Ú§Ö (Closure), §Ü§à§ä§à§â§à§Ö §Ó§à§Ù§Ó§â§Ñ§ë§Ñ§Ö§ä
§ï§Ü§Ù§Ö§Þ§á§Ý§ñ§â §Ü§Ý§Ñ§ã§ã§Ñ
$this->app->bind('HelpSpotAPI', function ($app) {
return new HelpSpotAPI($app['HttpClient']);
});
§±§à§Ý§å§é§Ñ§Ö§Þ §ã§Ñ§Þ §Ü§à§ß§ä§Ö§Û§ß§Ö§â §Ó §Ó§Ú§Õ§Ö §Ñ§â§Ô§å§Þ§Ö§ß§ä§Ñ ?§â§Ö§Ù§à§Ý§Ó§Ö§â§Ñ?
§©§Ñ§ä§Ö§Þ §Þ§à§Ø§ß§à §Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ä§î §Ü§à§ß§ä§Ö§Û§ß§Ö§â, §é§ä§à§Ò§í §á§à§Ý§å§é§Ñ§ä§î §á§à§Õ-
§Ù§Ñ§Ó§Ú§ã§Ú§Þ§à§ã§ä§Ú §ã§à§Ù§Õ§Ñ§Ó§Ñ§Ö§Þ§à§Ô§à §à§Ò§ì§Ö§Ü§ä§Ñ
§³§Ó§ñ§Ù§í§Ó§Ñ§ß§Ú§Ö §ã§Ú§ß§Ô§Ý§ä§à§ß§Ñ
singleton()
§á§â§Ú§Ó§ñ§Ù§í§Ó§Ñ§Ö§ä §Ü§Ý§Ñ§ã§ã §Ú§Ý§Ú §Ú§ß§ä§Ö§â§æ§Ö§Û§ã §Ü §Ü§à§ß§ä§Ö§Û§ß§Ö§â§å
§Õ§à§Ý§Ø§Ö§ß §Ò§í§ä§î §ã§à§Ù§Õ§Ñ§ß §ä§à§Ý§î§Ü§à §à§Õ§Ú§ß §â§Ñ§Ù
$this->app->singleton('FooBar', function ($app) {
return new FooBar($app['SomethingElse']);
});
§³§Ó§ñ§Ù§í§Ó§Ñ§ß§Ú§Ö §ã§å§ë§Ö§ã§ä§Ó§å§ð§ë§Ö§Ô§à §ï§Ü§Ù§Ö§Þ§á§Ý§ñ§â§Ñ §Ü§Ý§Ñ§ã§ã§Ñ §ã
§Ü§à§ß§ä§Ö§Û§ß§Ö§â§à§Þ
instance()
§±§â§Ú§Ó§ñ§Ù§Ü§Ñ §ã§å§ë§Ö§ã§ä§Ó§å§ð§ë§Ö§Ô§à §ï§Ü§Ù§Ö§Þ§á§Ý§ñ§â§Ñ §Ü §Ü§à§ß§ä§Ö§Û§ß§Ö§â§å
§ï§Ü§Ù§Ö§Þ§á§Ý§ñ§â §Ò§å§Õ§Ö§ä §Ó§ã§Ö§Ô§Õ§Ñ §Ó§à§Ù§Ó§â§Ñ§ë§Ñ§ä§î§ã§ñ §á§â§Ú §á§à§ã§Ý§Ö§Õ§å§ð§ë§Ú§ç
§à§Ò§â§Ñ§ë§Ö§ß§Ú§ñ§ç §Ü §Ü§à§ß§ä§Ö§Û§ß§Ö§â§å
$fooBar = new FooBar(new SomethingElse);
$this->app->instance('FooBar', $fooBar);
§³§Ó§ñ§Ù§í§Ó§Ñ§ß§Ú§Ö §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ §ã §â§Ö§Ñ§Ý§Ú§Ù§Ñ§è§Ú§Ö§Û
<?php
namespace AppHandlersCommands;
use AppCommandsCreateOrder;
use PusherClient as PusherClient;
class CreateOrderHandler {
/**
* §¿§Ü§Ù§Ö§Þ§á§Ý§ñ§â §Ü§Ý§Ú§Ö§ß§ä§Ñ Pusher SDK.
*/
protected $pusher;
public function __construct(PusherClient $pusher)
{
$this->pusher = $pusher;
}
<?php
namespace AppContracts;
interface EventPusher {
/**
* Push a new event to all clients.
*
* @param string $event
* @param array $data
* @return void
*/
public function push($event, array $data);
}
$this->app->bind('AppContractsEventPusher', 'AppServicesRedisEventPusher');
<?php
namespace AppHandlersCommands;
use AppContractsEventPusher;
class CreateOrderHandler {
/**
* §¿§Ü§Ù§Ö§Þ§á§Ý§ñ§â §Ü§Ý§Ú§Ö§ß§ä§Ñ Pusher SDK.
*/
protected $pusher;
/**
* §³§à§Ù§Õ§Ñ§ß§Ú§Ö §ß§à§Ó§à§Ô§à §ï§Ü§Ù§Ö§Þ§á§Ý§ñ§â§Ñ §Ü§Ý§Ñ§ã§ã§Ñ.
*
* @param EventPusher $pusher
* @return void
*/
public function __construct(EventPusher $pusher)
{
$this->pusher = $pusher;
}
§¬§à§ß§ä§Ö§Ü§ã§ä§ß§à§Ö §ã§Ó§ñ§Ù§í§Ó§Ñ§ß§Ú§Ö
$this->app->when('AppHandlersCommandsCreateOrderHandler')
->needs('AppContractsEventPusher')
->give('AppServicesPubNubEventPusher');
$this->app->when('AppHandlersCommandsCreateOrderHandler')
->needs('AppContractsEventPusher')
->give(function () {
// §ª§Ù§Ó§Ý§Ö§é§Ö§ß§Ú§Ö §Ù§Ñ§Ó§Ú§ã§Ú§Þ§à§ã§ä§Ú...
});
§±§à§Ý§å§é§Ö§ß§Ú§Ö §Ú§Ù §Ü§à§ß§ä§Ö§Û§ß§Ö§â§Ñ
$fooBar = $this->app->make('FooBar');
$fooBar = $this->app['FooBar']; // ArrayAccess
public function __construct(EventPusher $pusher)
§£§Ñ§â§Ú§Ñ§ß§ä§í
§³§à§Ò§í§ä§Ú§ñ §Ü§à§ß§ä§Ö§Û§ß§Ö§â§Ñ
$this->app->resolving(function ($object, $app) {
// §£§í§Ù§í§Ó§Ñ§Ö§ä§ã§ñ §á§â§Ú §Ú§Ù§Ó§Ý§Ö§é§Ö§ß§Ú§Ú §à§Ò§ì§Ö§Ü§ä§Ñ §Ý§ð§Ò§à§Ô§à §ä§Ú§á§Ñ...
});
$this->app->resolving(FooBar::class, function (FooBar $fooBar, $app) {
// §£§í§Ù§í§Ó§Ñ§Ö§ä§ã§ñ §á§â§Ú §Ú§Ù§Ó§Ý§Ö§é§Ö§ß§Ú§Ú §à§Ò§ì§Ö§Ü§ä§Ñ §ä§Ú§á§Ñ "FooBar"...
});

More Related Content

§Ó§Ó§Ö§Õ§Ö§ß§Ú§Ö §Ó Laravel 5