if (App::isLocal()) { echo "environment=local\n"; } elseif (App::runningUnitTests()) { echo "environment=testing\n"; }
atau
if (App::environment('production', 'staging'))
{
echo "I'm on production or staging\n";
}
else
{
echo "environment=", App::environment(), "\n";
}
atau
echo app('env');
2. Semak samada Running di Console :
if (App::runningInConsole()) { echo "I'm in the console, baby!"; }
nota : laravel guna command php iniphp_sapi_name()
equals'cli'
3. Simpan nilai dalam IoC Container :
- ini akan menjadikan nilai boleh dicapai bila2 masa dalam aplikasi tersebut.
$is_evening = (date('G') > 18) ? true : false; App::instance('myapp.evening', $is_evening);
cara untuk capai :
App::make('myapp.evening')
atau app('myapp.evening')
.
4. Lekatkan Interface kepada Implementation :
// Bind as an alias App::bind('FooInterface', 'FooClass'); // Bind as a closure App::bind('FooInterface', function($app) { return new FooClass; });
cara panggilan :
App::make('FooInterface')
5. Objek dari IoC Container :
// Somewhere in your code App::bind('myclass', function($app) { return new MyCoolClass(); }); // Later $myclass = App::make('myclass');
atau :
// Somewhere in your code
App::instance('mydata', $mydata);
// Later
$mydata = App::make('mydata');
- dapatkan :
// Somewhere in your code
App::singleton('mysingleton', 'stdClass');
// Later
$var = App::make('mysingleton');
$var->test = '123';
// Even later
$var2 = App::make('mysingleton');
echo $var2->test;
Lain2 contoh :
class Foo {
}
class Bar {
protected $foo;
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
}
// Construct Bar, automatically injecting a Foo instance
$bar = App::make('Bar');
App::bind('SomeInterface', 'SomeClassImplementingSomeInterface');
$var = App::make('SomeInterface');
atau
$obj = app('stdClass'); // alias kepada App::make()
.
6. Daftar Filter sebelum Application :
App::before(function($request) { if ($request->ajax()) { // Returning a value will short-circut the life cycle and // keep any requests from being processed further return Response::json(['error' => 'AJAX not allowed']); } // No return value allows processing to continue as normal });
biasa dalam fail :
app/filters.php
7. Daftar Filter selepas Application :
App::after(function($request, $response) { // Capture last response $content = $response->getContent(); File::put(storage_path().'/logs/last_response.txt', $content); });
8. Daftar Shutdown Callback :
App::shutdown(function() { // gets called right before app quits. });
dipangil semasa process akhir, sebelum keluar, biasa diguna untuk mematikan servis dan log.
9. Panggil Shutdown Callbacks :
// Call registered callbacks App::shutdown();
nota : keluar secara luarbiasa.
10. Semak samada Applikasi di Booted :
// Usually in a service provider if (App::isBooted()) { // Take action when booted }
11. Daftar Booting atau Booted Callbacks :
App::booted(function($app) { // Code to execute right before the app is booted. }); App::booting(function($app) { // Code to execute right after the app is booted. })
12. Daftar HttpKernel Middleware :
App::middleware('MyApp\Middleware');
// 1 arg App::middleware('MyApp\Middleware', array($arg1)); // 2+ args App::middleware('MyApp\Middleware', array($arg1, $arg2));
13. Buang Middleware dari Application :
// Remove a class you've already registered App::removeMiddleware('MyApp\Middleware'); // Remove the FrameGuard middleware App::removeMiddleware('Illuminate\Http\FrameGuard');
14. Semak samada Application sedang dalam penyelenggaraan :
if (App::isDownForMaintenance()) { die("We're currently down for maintenance."); }
15. Daftar mod penyelenggaraan :
App::down(function() { return Response::view('maintenance.mode', array(), 503); });
16. Dapatkan fail2 bootstrap :
echo App::getBootstrapFile();
17. Daftar Service Provider :
Pilihan 1 :
tambah dalam fail : app/config/app.php
'providers' => array( ... 'MyApp\Providers\MyServiceProvider', ),
Pilahan 2 :
App::register('MyApp\Providers\MyServiceProvider'); // atau $provider = new MyApp\Providers\MyServiceProvider; App::register($provider);
18. Daftar Finish Callback :
App::finish(function($request, $response) { // Use request and/or response to do logging or some after // main processing stuff });
daftarr dalam : app/start/global.php
19. Pastikan Application sedia untuk Responses :
// In a service provider if (App::readyForResponses()) { // Take action when booted }
20. HttpExceptions :
- Bila tak jumpa :
App::abort(404);
atau
App::abort(404, 'User not found');
// Unauthorized
App::abort(401, 'Not authenticated');
// Forbidden
App::abort(403, 'Access denied');
// Internal Server Error
App::abort(500, 'Something bad happened');
// Not implemented
App::abort(501, 'Feature not implemented');
atau
App::abort(401, 'Not authenticated', ['WWW-Authenticate' => 'Basic']);
21. Daftar 404 Error Handler :
App::missing(function($exception) { return View::make('not-found')->withMessage($exception->getMessage()); });
dalam : app/start/global.php
.
22. Daftar Error Handler :
App::error(function($exception) { die('ERROR: '.$exception->getMessage()); });
atau
App::error(function(RuntimeException $exception)
{
return View::make('error', compact('exception'));
});
23. Daftar "Last Chance" Error Handler :
- handle error hanya bila tiada yang handle
App::pushError(function($exception) { die('ERROR: '.$exception->getMessage()); });
24. Daftar Error Handler for Fatal Errors :
App::fatal(function($exception) { die('FATAL ERROR: '.$exception->getMessage()); });
Fatal errors adalah dari PHP : E_ERROR
, E_CORE_ERROR
, E_COMPILE_ERROR
, or E_PARSE
.
25. Dapatkan Application Locale :
if (App::getLocale() == 'en') { echo "It's English!"; }
sama dengan :
Config::get('app.locale')
26. Set Application Locale semasa :
// Change to Spanish App::setLocale('es');
Nota :
- ubah
app.locale
. - Set locale translator.
locale.changed
event dimualakan.
27. Dapatkan Default Request Class :echo "Request Class = ", App::requestClass();
28. Set Request untuk persekiran Console :
App::setRequestForConsoleEnvironment();
29. Panggil Static Method semasa Default Request Class :
$request = App::onRequest('createFromGlobals');
lain2 method :
createFromBase(SymfonyRequest $request)
- Create an Illuminate request from a Symfony instance.createFromGlobals()
- Creates a new request with values from PHP's super globals.create($uri, $method, $parameters, $cookies, $files, $server, $content)
- Creates a Request based on a given URI and configuration.setFactory($callable)
- Sets a callable able to create a Request instance.setTrustedProxies(array $proxies)
- Sets a list of trusted proxies.getTrustedProxies()
- Gets the list of trusted proxies.setTrustedHosts(array $hostPatterns)
- Sets a list of trusted host patterns.getTrustedHosts()
- Gets the list of trusted host patterns.setTrustedHeaderName($key, $value)
- Sets the name for trusted headers.getTrustedHeaderName($key)
- Get the trusted proxy header name.normalizeQueryString($qs)
- Normalize a query string.enableHttpMethodParameterOverride()
- Enables support for the _method request parameter to determine the intended HTTP method.getHttpMethodParameterOverride()
- Checks whether support for the _method request parameter is enabled.
No comments:
Post a Comment