forked from JesseObrien/laravel-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
554 lines (516 loc) · 17.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/foundation/4.3.1/css/foundation.min.css" />
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet" />
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/assets/css/page.css" />
<title>Laravel Cheat Sheet</title>
</head>
<body>
<nav class="top-bar">
<ul class="title-area">
<li class="name"><h1><a href="#">Laravel Cheat Sheet</a></h1></li>
<li class="toggle-topbar menu-icon"><a href="#"><span>Menu</span></a></li>
</ul>
<section class="top-bar-section">
<ul class="left">
<li class="comments-toggle"><a href="#"><i class="icon-comments"></i> Toggle Code Comments</a></li>
<li><a href="http://github.com/jesseobrien/laravel-cheatsheet"><i class="icon-github"></i> Github</a></li>
</ul>
</section>
</nav>
<div class="row">
<div class="large-6 columns code-column">
<h4>Routing</h4>
<pre class="prettyprint lang-php">Route::get('foo', function(){});
Route::get('foo', 'ControllerName@function');
</pre>
<h6>Triggering Errors</h6>
<pre class="prettyprint lang-php">App::abort(404);
throw new NotFoundHttpException;
</pre>
<h6>Route Parameters</h6>
<pre class="prettyprint lang-php">Route::get('foo/{bar}', function($bar){});
Route::get('foo/{bar?}', function($bar = 'bar'){});
</pre>
<h6>HTTP Verbs</h6>
<pre class="prettyprint lang-php">Route::any('foo', function(){});
Route::post('foo', function(){});
Route::patch('foo', function(){});
Route::delete('foo', function(){});
</pre>
<h6>Secure Routes</h6>
<pre class="prettyprint lang-php">Route::get('foo', array('https', function(){}));</pre>
<h6>Route Constraints</h6>
<pre class="prettyprint lang-php">Route::get('foo/{bar}', function($bar){})
->where('bar', '[0-9]+');
Route::get('foo/{bar}/{baz}', function($bar, $baz){})
->where(array('bar' => '[0-9]+', 'baz' => '[A-Za-z]'))
</pre>
<h6>Filters</h6>
<pre class="prettyprint lang-php">Route::filter('auth', function(){}); // Declare an auth filter
Route::filter('foo', 'FooFilter'); // Register a class as a filter
Route::get('foo', array('before' => 'auth', function(){}));
// Routes in this group are guarded by the 'auth' filter
Route::group(array('before' => 'auth'), function(){});
Route::when('foo/*', 'foo'); // Pattern filter
Route::when('foo/*', 'foo', array('post')); // HTTP verb pattern
</pre>
<h6>Named Routes</h6>
<pre class="prettyprint lang-php">Route::currentRouteName();
Route::get('foo/bar', array('as' => 'foobar', function(){}));
</pre>
<h6>Route Prefixing</h6>
<pre class="prettyprint lang-php">// This route group will carry the prefix 'foo'
Route::group(array('prefix' => 'foo'), function(){})
</pre>
<h6>Sub-Domain Routing</h6>
<pre class="prettyprint lang-php">// {sub} will be passed to the closure
Route::group(array('domain' => '{sub}.example.com'), function(){});
</pre>
<h4>URLs</h4>
<pre class="prettyprint lang-php">URL::full();
URL::current();
URL::previous();
URL::to('foo/bar', $parameters, $secure);
URL::action('FooController@method', $parameters, $absolute);
URL::route('foo', $parameters, $absolute);
URL::secure('foo/bar', $parameters);
URL::asset('css/foo.css', $secure);
URL::secureAsset('css/foo.css');
URL::isValidUrl('http://example.com');
URL::getRequest();
URL::setRequest($request);
URL::getGenerator();
URL::setGenerator($generator);
</pre>
<h4>Views</h4>
<pre class="prettyprint lang-php">View::make('path/to/view');
View::make('foo/bar')->with('key', 'value');
View::make('foo/bar', array('key' => 'value'));
// Share a value across all views
View::share('key', 'value');
// Nesting views
View::make('foo/bar')->nest('name', 'foo/baz', $data);
// Register a view composer
View::composer('viewname', function($view){});
//Register multiple views to a composer
View::composer(array('view1', 'view2'), function($view){});
// Register a composer class
View::composer('viewname', 'FooComposer');
View::creator('viewname', function($view){});
</pre>
<h4>Blade Templates</h4>
<pre class="prettyprint lang-php">@extends('layout.name')
@section('name') // Begin a section
@stop // End a section
@yield('name') // Show a section name in a template
@include('view.name')
@include('view.name', array('key' => 'value'));
@lang('messages.name')
@choice('messages.name', 1);
@if
@else
@elseif
@endif
@unless
@endunless
@for
@endfor
@foreach
@endforeach
@while
@endwhile
{{ $var }} // Echo content
{{{ $var }}} // Echo escaped content
{{-- Blade Comment --}}
</pre>
<h4>Forms</h4>
<pre class="prettyprint lang-php">Form::open(array('url' => 'foo/bar', 'method' => 'PUT'));
Form::open(array('route' => 'foo.bar'));
Form::open(array('route' => 'foo.bar', $parameter));
Form::open(array('action' => 'FooController@method'));
Form::open(array('action' => 'FooController@method', $parameter));
Form::open(array('url' => 'foo/bar', 'files' => true));
Form::token();
Form::model($foo, array('route' => 'foo.bar', $foo->bar));
</pre>
<h6>Form Elements</h6>
<pre class="prettyprint lang-php">Form::label('id', 'Description');
Form::label('id', 'Description', array('class' => 'foo'));
Form::text('name');
Form::text('name', $value);
Form::hidden('foo', $value);
Form::password('password');
Form::email('name', $value, array());
Form::file('name', array());
Form::checkbox('name', 'value');
Form::radio('name', 'value');
Form::select('name', array('key' => 'value'));
Form::select('name', array('key' => 'value'), 'key');
Form::submit('Submit!');
Form::macro('fooField', function()
{
return '<input type="custom"/>';
});
Form::fooField();
</pre>
<h4>HTML Builder</h4>
<pre class="prettyprint lang-php">HTML::macro('name', function(){});
HTML::entities($value);
HTML::decode($value);
HTML::script($url, $attributes);
HTML::style($url, $attributes);
HTML::link($url, 'title', $attributes, $secure);
HTML::secureLink($url, 'title', $attributes);
HTML::linkAsset($url, 'title', $attributes, $secure);
HTML::linkSecureAsset($url, 'title', $attributes);
HTML::linkRoute($name, 'title', $parameters, $attributes);
HTML::linkAction($action, 'title', $parameters, $attributes);
HTML::mailto($email, 'title', $attributes);
HTML::email($email);
HTML::ol($list, $attributes);
HTML::ul($list, $attributes);
HTML::listing($type, $list, $attributes);
HTML::listingElement($key, $type, $value);
HTML::nestedListing($key, $type, $value);
HTML::attributes($attributes);
HTML::attributeElement($key, $value);
HTML::obfuscate($value);
</pre>
<h4>Events</h4>
<pre class="prettyprint lang-php">Event::fire('foo.bar', array($bar));
Event::listen('foo.bar', function($bar){});
Event::listen('foo.*', function($bar){});
Event::listen('foo.bar', 'FooHandler', 10);
Event::listen('foo.bar', 'BarHandler', 5);
Event::listen('foor.bar', function($event){ return false; });
Event::queue('foo', array($bar));
Event::flusher('foo', function($bar){});
Event::flush('foo');
Event::subscribe(new FooEventHandler);
</pre>
<h4>Eloquent</h4>
<pre class="prettyprint lang-php">Model::create(array('key' => 'value'));
Model::destroy(1);
Model::all();
Model::find(1);
Model::findOrFail(1); // Trigger an exception
Model::where('foo', '=', 'bar')->get();
Model::where('foo', '=', 'bar')->first();
Model::where('foo', '=', 'bar')->firstOrFail(); // Exception
Model::where('foo', '=', 'bar')->count();
Model::where('foo', '=', 'bar')->delete();
Model::whereRaw('foo = bar and cars = 2', array(20))->get();
Model::on('connection-name')->find(1);
Model::with('relation')->get();
</pre>
<h6>Soft Delete</h6>
<pre class="prettyprint lang-php">Model::withTrashed()->where('cars', 2)->get();
Model::withTrashed()->where('cars', 2)->restore();
Model::where('cars', 2)->forceDelete();
Model::onlyTrashed()->where('cars', 2)->get();
</pre>
<h6>Events</h6>
<pre class="prettyprint lang-php">Model::creating(function($model){});
Model::created(function($model){});
Model::updating(function($model){});
Model::updated(function($model){});
Model::saving(function($model){});
Model::saved(function($model){});
Model::deleting(function($model){});
Model::deleted(function($model){});
Model::observe(new FooObserver);
</pre>
<h4>Mail</h4>
<pre class="prettyprint lang-php">Mail::send('email.view', $data, function($message){});
Mail::send(array('html.view', 'text.view'), $data, $callback);
Mail::queue('email.view', $data, function($message){});
Mail::queueOn('queue-name', 'email.view', $data, $callback);
Mail::later(5, 'email.view', $data, function($message){});
Mail::pretend(); // Write all email to logs instead of sending
</pre>
<h4>Queues</h4>
<pre class="prettyprint lang-php">Queue::push('SendMail', array('message' => $message));
Queue::push('SendEmail@send', array('message' => $message));
Queue::push(function($job) use $id {});
php artisan queue:listen
php artisan queue:listen connection
php artisan queue:listen --timeout=60
php artisan queue:work
</pre>
<h4>Localization</h4>
<pre class="prettyprint lang-php">App::setLocale('en');
Lang::get('messages.welcome');
Lang::get('messages.welcome', array('foo' => 'Bar'));
Lang::has('messages.welcome');
Lang::choice('messages.apples', 10);
</pre>
</div>
<div class="large-6 columns code-column">
<h4>Input</h4>
<pre class="prettyprint lang-php">Input::get('key');
Input::get('key', 'default'); // Default if the key is missing
Input::has('key');
Input::all();
// Only retrieve 'foo' and 'bar' when getting input
Input::only('foo', 'bar');
// Disregard 'foo' when getting input
Input::except('foo');
</pre>
<h6>Session Input (flash)</h6>
<pre class="prettyprint lang-php">// Flash input to the session
Input::flash();
Input::flashOnly('foo', 'bar');
Input::flashExcept('foo', 'baz');
Input::old('key');
</pre>
<h6>Files</h6>
<pre class="prettyprint lang-php">// Use a file that's been uploaded
Input::file('filename');
// Determine if a file was uploaded
Input::hasFile('filename');
// Access file properties
Input::file('name')->getRealPath();
Input::file('name')->getClientOriginalName();
Input::file('name')->getSize();
Input::file('name')->getMimeType();
// Move an uploaded file
Input::file('name')->move($destinationPath);
// Move an uploaded file
Input::file('name')->move($destinationPath, $fileName);
</pre>
<h4>Cache</h4>
<pre class="prettyprint lang-php">Cache::put('key', 'value', $minutes);
Cache::add('key', 'value', $minutes);
Cache::forever('key', 'value');
Cache::remember('key', $minutes, function(){ return 'value' });
Cache::rememberForever('key', function(){ return 'value' });
Cache::forget('key');
Cache::has('key');
Cache::get('key');
Cache::get('key', 'default');
Cache::get('key', function(){ return 'default'; });
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
Cache::section('group')->put('key', $value);
Cache::section('group')->get('key');
Cache::section('group')->flush();
</pre>
<h4>Cookies</h4>
<pre class="prettyprint lang-php">Cookie::get('key');
// Create a cookie that lasts for ever
Cookie::forever('key', 'value');
// Send a cookie with a response
$response = Response::make('Hello World');
$response->withCookie(Cookie::make('name', 'value', $minutes));
</pre>
<h4>Sessions</h4>
<pre class="prettyprint lang-php">Session::get('key');
Session::get('key', 'default');
Session::get('key', function(){ return 'default'; });
Session::put('key', 'value');
Session::all();
Session::has('key');
Session::forget('key');
Session::flush();
Session::regenerate();
Session::flash('key', 'value');
Session::reflash();
Session::keep(array('key1', 'key2'));
</pre>
<h4>Requests</h4>
<pre class="prettyprint lang-php">Request::path();
Request::is('foo/*');
Request::url();
Request::segment(1);
Request::header('Content-Type');
Request::server('PATH_INFO');
Request::ajax();
Request::secure();
</pre>
<h4>Responses</h4>
<pre class="prettyprint lang-php">return Response::make($contents);
return Response::make($contents, 200);
return Response::json(array('key' => 'value'));
return Response::json(array('key' => 'value'))
->setCallback(Input::get('callback'));
return Response::download($filepath);
return Response::download($filepath, $filename, $headers);
// Create a response and modify a header value
$response = Response::make($contents, 200);
$response->header('Content-Type', 'application/json');
return $response;
// Attach a cookie to a response
return Response::make($content)
->withCookie(Cookie::make('key', 'value'));
</pre>
<h4>Redirects</h4>
<pre class="prettyprint lang-php">return Redirect::to('foo/bar');
return Redirect::to('foo/bar')->with('key', 'value');
return Redirect::to('foo/bar')->withInput(Input::get());
return Redirect::to('foo/bar')->withInput(Input::except('password'));
return Redirect::to('foo/bar')->withErrors($validator);
return Redirect::route('foobar');
return Redirect::route('foobar', array('value'));
return Redirect::route('foobar', array('key' => 'value'));
return Redirect::action('FooController@index');
return Redirect::action('FooController@baz', array('value'));
return Redirect::action('FooController@baz', array('key' => 'value'));
</pre>
<h4>IoC</h4>
<pre class="prettyprint lang-php">App::bind('foo', function($app){ return new Foo; });
App::make('foo');
App::make('FooBar'); // If this class exists, it's returned
App::singleton('foo', function(){ return new Foo; });
App::instance('foo', new Foo);
App::bind('FooRepositoryInterface', 'BarRepository');
App::register('FooServiceProvider');
App::resolving(function($object){}); // Listen for object resolution
</pre>
<h4>Security</h4>
<h6>Passwords</h6>
<pre class="prettyprint lang-php">Hash::make('secretpassword');
Hash::check('secretpassword', $hashedPassword);
Hash::needsRehash($hashedPassword);
</pre>
<h6>Auth</h6>
<pre class="prettyprint lang-php">Auth::check(); // Check if the user is logged in
Auth::user();
Auth::attempt(array('email' => $email, 'password' => $password));
Auth::attempt($credentials, true); // Remember user login
Auth::once($credentials); // Log in for a single request
Auth::login(User::find(1));
Auth::loginUsingId(1);
Auth::logout();
Auth::validate($credentials);
Auth::basic('username');
Auth::onceBasic();
Password::remind($credentials, function($message, $user){});
</pre>
<h6>Encryption</h6>
<pre class="prettyprint lang-php">Crypt::encrypt('secretstring');
Crypt::decrypt($encryptedString);
Crypt::setMode('ctr');
Crypt::setCipher($cipher);
</pre>
<h4>Validation</h4>
<pre class="prettyprint lang-php">Validator::make(
array('key' => 'Foo'),
array('key' => 'required|in:Foo')
);
Validator::extend('foo', function($attribute, $value, $params){});
Validator::extend('foo', 'FooValidator@validate');
Validator::resolver(function($translator, $data, $rules, $msgs)
{
return new FooValidator($translator, $data, $rules, $msgs);
});
</pre>
<h6>Rules</h6>
<pre class="prettyprint lang-php">accepted
active_url
after:YYYY-MM-DD
before:YYYY-MM-DD
alpha
alpha_num
between:1,10
confirmed
date
date_format:YYYY-MM-DD
different:fieldname
email
exists:table,column
image
in:foo,bar,baz
not_in:foo,bar,baz
integer
numeric
ip
max:value
min:value
mimes:jpeg,png
regex:[0-9]
required
required_if:field,value
required_with:foo,bar,baz
required_without:foo,bar,baz
same:field
size:value
</pre>
<h4>Helpers</h4>
<h6>Arrays</h6>
<pre class="prettyprint lang-php">array_add($array, 'key', 'value');
array_build($array, function(){});
array_divide($array);
array_dot($array);
array_except($array, aray('key'));
array_fetch($array, 'key');
array_first($array, function($key, $value){}, $default);
array_flatten($array); // Strips keys from the array
array_forget($array, 'foo');
array_forget($array, 'foo.bar'); // Dot notation
array_get($array, 'foo', 'default');
array_get($array, 'foo.bar', 'default');
array_only($array, array('key'));
array_pluck($array, 'key'); // Return array of key => values
array_pull($array, 'key'); // Return and remove 'key' from array
array_set($array, 'key', 'value');
array_set($array, 'key.subkey', 'value'); // Dot notation
array_sort($array, function(){});
head($array); // First element of an array
last($array); // Last element of an array
</pre>
<h6>Paths</h6>
<pre class="prettyprint lang-php">app_path();
public_path();
base_path(); // App root path
storage_path();
</pre>
<h6>Strings</h6>
<pre class="prettyprint lang-php">
camel_case($value);
class_basename($class);
e('<html>'); // Escape a string
starts_with('Foo bar.', 'Foo');
ends_with('Foo bar.', 'bar.');
snake_case('fooBar');
str_contains('Hello foo bar.', 'foo');
str_finish('foo/bar', '/'); // Result: foo/bar/
str_is('foo*', 'foobar');
str_plural('car');
str_random(25);
str_singular('cars');
studly_case('foo_bar'); // Result: FooBar
trans('foo.bar');
trans_choice('foo.bar', $count);
</pre>
<h6>URLs and Links</h6>
<pre class="prettyprint lang-php">action('FooController@method', $parameters);
link_to('foo/bar', $title, $attributes, $secure);
link_to_asset('img/foo.jpg', $title, $attributes, $secure);
link_to_route('route.name', $title, $parameters, $attributes);
link_to_action('FooController@method', $title, $params, $attrs);
asset('img/photo.jpg', $title, $attributes); // HTML Link
secure_asset('img/photo.jpg', $title, $attributes); // HTTPS link
secure_url($path, $parameters);
route($route, $parameters, $absolute = true);
url($path, $parameters = array(), $secure = null);
</pre>
<h6>Miscellaneous</h6>
<pre class="prettyprint lang-php">
csrf_token();
dd($value);
value(function(){ return 'bar'; });
with(new Foo)->chainedMethod();
</pre>
</div>
</div>
<script src="/assets/js/jquery-1.10.2.min.js"></script>
<script src="/assets/js/google-code-prettify/prettify.js"></script>
<script type="text/javascript" src="/assets/js/app.js"></script>
</body>
</html>