-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slim Framework Documentation.html
3440 lines (2730 loc) · 145 KB
/
Slim Framework Documentation.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Slim Framework Documentation</title>
<link rel="stylesheet" href="/bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" href="/bootstrap/css/responsive.css"/>
<link rel="stylesheet" href="/styles/css/all.css"/>
</head>
<body>
<div class="navbar navbar-inverse navbar-static-top">
<div class="navbar-inner">
<a class="brand" href="http://www.slimframework.com" target="_blank">Slim Framework Documentation</a>
<ul class="nav pull-right visible-tablet visible-desktop">
<li><a href="http://www.slimframework.com" target="_blank">Visit Website <i class="icon-arrow-right"></i></a></li>
</ul>
</div>
</div>
<div class="container-fluid pts">
<div class="row-fluid">
<div class="span4">
<div class="well well-small">
<ul class="nav nav-list">
<li class="nav-header"><a href="#Getting-Started">Getting Started</a></li>
<li class="nav-page"><a href="#Installation">Installation</a></li>
<li class="nav-page"><a href="#System-Requirements">System Requirements</a></li>
<li class="nav-page"><a href="#Hello-World">Hello World</a></li>
<li class="nav-header"><a href="#Configuration">Configuration</a></li>
<li class="nav-page"><a href="#Configuration-Overview">Configuration Overview</a></li>
<li class="nav-page"><a href="#Application-Settings">Application Settings</a></li>
<li class="nav-page"><a href="#Application-Names-and-Scopes">Application Names and Scopes</a></li>
<li class="nav-page"><a href="#Application-Modes">Application Modes</a></li>
<li class="nav-header"><a href="#Routing">Routing</a></li>
<li class="nav-page"><a href="#Routing-Overview">Routing Overview</a></li>
<li class="nav-page"><a href="#GET-Routes">GET Routes</a></li>
<li class="nav-page"><a href="#POST-Routes">POST Routes</a></li>
<li class="nav-page"><a href="#PUT-Routes">PUT Routes</a></li>
<li class="nav-page"><a href="#DELETE-Routes">DELETE Routes</a></li>
<li class="nav-page"><a href="#OPTIONS-Routes">OPTIONS Routes</a></li>
<li class="nav-page"><a href="#Custom-HTTP-Methods">Custom HTTP Methods</a></li>
<li class="nav-page"><a href="#Route-Parameters">Route Parameters</a></li>
<li class="nav-page"><a href="#Route-Names">Route Names</a></li>
<li class="nav-page"><a href="#Route-Conditions">Route Conditions</a></li>
<li class="nav-page"><a href="#Route-Middleware">Route Middleware</a></li>
<li class="nav-page"><a href="#Route-Helpers">Route Helpers</a></li>
<li class="nav-page"><a href="#Route-URL-Rewriting">Route URL Rewriting</a></li>
<li class="nav-header"><a href="#Environment">Environment</a></li>
<li class="nav-page"><a href="#Environment-Overview">Environment Overview</a></li>
<li class="nav-header"><a href="#Request">Request</a></li>
<li class="nav-page"><a href="#Request-Overview">Request Overview</a></li>
<li class="nav-page"><a href="#Request-Method">Request Method</a></li>
<li class="nav-page"><a href="#Request-Headers">Request Headers</a></li>
<li class="nav-page"><a href="#Request-Body">Request Body</a></li>
<li class="nav-page"><a href="#Request-Variables">Request Variables</a></li>
<li class="nav-page"><a href="#Request-Cookies">Request Cookies</a></li>
<li class="nav-page"><a href="#Request-Paths">Request Paths</a></li>
<li class="nav-page"><a href="#XMLHttpRequest">XMLHttpRequest</a></li>
<li class="nav-page"><a href="#Request-Helpers">Request Helpers</a></li>
<li class="nav-header"><a href="#Response">Response</a></li>
<li class="nav-page"><a href="#Response-Overview">Response Overview</a></li>
<li class="nav-page"><a href="#Response-Status">Response Status</a></li>
<li class="nav-page"><a href="#Response-Headers">Response Headers</a></li>
<li class="nav-page"><a href="#Response-Body">Response Body</a></li>
<li class="nav-page"><a href="#Response-Cookies">Response Cookies</a></li>
<li class="nav-page"><a href="#Response-Helpers">Response Helpers</a></li>
<li class="nav-header"><a href="#View">View</a></li>
<li class="nav-page"><a href="#View-Overview">View Overview</a></li>
<li class="nav-page"><a href="#Rendering">Rendering</a></li>
<li class="nav-page"><a href="#Custom-Views">Custom Views</a></li>
<li class="nav-page"><a href="#View-Data">View Data</a></li>
<li class="nav-header"><a href="#HTTP-Caching">HTTP Caching</a></li>
<li class="nav-page"><a href="#HTTP-Caching-Overview">HTTP Caching Overview</a></li>
<li class="nav-page"><a href="#ETag">ETag</a></li>
<li class="nav-page"><a href="#Last-Modified">Last Modified</a></li>
<li class="nav-page"><a href="#Expires">Expires</a></li>
<li class="nav-header"><a href="#Middleware">Middleware</a></li>
<li class="nav-page"><a href="#Middleware-Overview">Middleware Overview</a></li>
<li class="nav-page"><a href="#How-to-Use-Middleware">How to Use Middleware</a></li>
<li class="nav-page"><a href="#How-to-Write-Middleware">How to Write Middleware</a></li>
<li class="nav-header"><a href="#Hooks">Hooks</a></li>
<li class="nav-page"><a href="#Hooks-Overview">Hooks Overview</a></li>
<li class="nav-page"><a href="#How-to-Use-Hooks">How to Use Hooks</a></li>
<li class="nav-page"><a href="#Default-Hooks">Default Hooks</a></li>
<li class="nav-page"><a href="#Custom-Hooks">Custom Hooks</a></li>
<li class="nav-header"><a href="#Flash-Messages">Flash Messages</a></li>
<li class="nav-page"><a href="#Flash-Messaging-Overview">Flash Messaging Overview</a></li>
<li class="nav-page"><a href="#Flash-Next">Flash Next</a></li>
<li class="nav-page"><a href="#Flash-Now">Flash Now</a></li>
<li class="nav-page"><a href="#Flash-Keep">Flash Keep</a></li>
<li class="nav-header"><a href="#Sessions">Sessions</a></li>
<li class="nav-page"><a href="#Native-Session-Store">Native Session Store</a></li>
<li class="nav-page"><a href="#Cookie-Session-Store">Cookie Session Store</a></li>
<li class="nav-header"><a href="#Logging">Logging</a></li>
<li class="nav-page"><a href="#Overview">Overview</a></li>
<li class="nav-page"><a href="#Activate-Logging">Activate Logging</a></li>
<li class="nav-page"><a href="#Log-Levels">Log Levels</a></li>
<li class="nav-page"><a href="#Log-Writers">Log Writers</a></li>
<li class="nav-header"><a href="#Error-Handling">Error Handling</a></li>
<li class="nav-page"><a href="#Overview">Overview</a></li>
<li class="nav-page"><a href="#Error-Handler">Error Handler</a></li>
<li class="nav-page"><a href="#Not-Found-Handler">Not Found Handler</a></li>
<li class="nav-page"><a href="#Debugging">Debugging</a></li>
<li class="nav-page"><a href="#Output-Redirection">Output Redirection</a></li>
</ul>
</div>
</div>
<div class="span8">
<section class="page-section" id="Getting-Started">
<h1 class="page-header">Getting Started <a class="bookmark" href="http://docs.slimframework.com/#Getting-Started"><i class="icon-bookmark"></i></a></h1>
<article class="page-article" id="Installation">
<header>
<h2 class="page-article-header">Installation</h2>
</header>
<h3>Composer Install</h3>
<p>Install composer in your project:</p>
<pre><code>curl -s https://getcomposer.org/installer | php
</code></pre>
<p>Create a <code>composer.json</code> file in your project root:</p>
<pre><code>{
"require": {
"slim/slim": "2.*"
}
}
</code></pre>
<p>Install via composer:</p>
<pre><code>php composer.phar install
</code></pre>
<p>Add this line to your application’s <code>index.php</code> file:</p>
<pre><code><?php
require 'vendor/autoload.php';
</code></pre>
<h3>Manual Install</h3>
<p>Download and extract the Slim Framwork into your project directory and <code>require</code> it in your application’s <code>index.php</code>
file. You’ll also need to register Slim’s autoloader.</p>
<pre><code><?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
</code></pre>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="System-Requirements">
<header>
<h2 class="page-article-header">System Requirements</h2>
</header>
<ul>
<li>PHP >= 5.3.0</li>
</ul>
<p>The <code>mcrypt</code> extension is required <em>only</em> if you use encrypted cookies.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="Hello-World">
<header>
<h2 class="page-article-header">Hello World</h2>
</header>
<p>Instantiate a Slim application:</p>
<pre><code>$app = new \Slim\Slim();
</code></pre>
<p>Define a HTTP GET route:</p>
<pre><code>$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
</code></pre>
<p>Run the Slim application:</p>
<pre><code>$app->run();
</code></pre>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
</section>
<section class="page-section" id="Configuration">
<h1 class="page-header">Configuration <a class="bookmark" href="http://docs.slimframework.com/#Configuration"><i class="icon-bookmark"></i></a></h1>
<article class="page-article" id="Configuration-Overview">
<header>
<h2 class="page-article-header">Configuration Overview</h2>
</header>
<p>There are two ways to apply settings to the Slim application. First during Slim application instantiation and second
after instantiation. All settings can be applied at instatiation time by passing Slim’s constructor an associative
array. All settings can be retrieved and modified after instantiation, however some of them can not be done simply by
using the config application instance method but will be demonstrated as necessary below. Before I list the available
settings, I want to quickly explain how you may define and inspect settings with your Slim application.</p>
<h3>During Instantiation</h3>
<p>To define settings upon instantiation, pass an associative array into the Slim constructor.</p>
<pre><code><?php
$app = new Slim(array(
'debug' => true
));
</code></pre>
<h3>After Instantiation</h3>
<p>To define settings after instantiation, the majority can use the config application instance method; the first
argument is the setting name and the second argument is the setting value.</p>
<pre><code><?php
$app->config('debug', false);
</code></pre>
<p>You may also define multiple settings at once using an associative array:</p>
<pre><code><?php
$app->config(array(
'debug' => true,
'templates.path' => '../templates'
));
</code></pre>
<p>To retrieve the value of a setting, you also use the config application instance method; however, you only pass one
argument - the name of the setting you wish to inspect. If the setting you request does not exist, <code>null</code> is returned.</p>
<pre><code><?php
$settingValue = $app->config('templates.path'); //returns "../templates"
</code></pre>
<p>You are not limited to the settings shown below; you may also define your own.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="Application-Settings">
<header>
<h2 class="page-article-header">Application Settings</h2>
</header>
<h3>mode</h3>
<p>This is an identifier for the application’s current mode of operation. The mode does not affect a Slim application’s
internal functionality. Instead, the mode is only for you to optionally invoke your own code for a given mode with the
<code>configMode()</code> application method.</p>
<p>The application mode is declared during instantiation, either as an environment variable or as an argument to the
Slim application constructor. It cannot be changed afterward. The mode may be anything you want — “development”,
“test”, and “production” are typical, but you are free to use anything you want (e.g. “foo”).</p>
<pre><code><?php
$app = new \Slim\Slim(array(
'mode' => 'development'
));
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string</dd>
<dt>Default Value</dt>
<dd>“development”</dd>
</dl>
<h3>debug</h3>
<div class="alert alert-info">
<strong>Heads Up!</strong> Slim converts errors into `ErrorException` instances.
</div>
<p>If debugging is enabled, Slim will use its built-in error handler to display diagnostic information for uncaught
Exceptions. If debugging is disabled, Slim will instead invoke your custom error handler, passing it the otherwise
uncaught Exception as its first and only argument.</p>
<pre><code><?php
$app = new \Slim\Slim(array(
'debug' => true
));
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>boolean</dd>
<dt>Default Value</dt>
<dd>true</dd>
</dl>
<h3>log.writer</h3>
<p>Use a custom log writer to direct logged messages to the appropriate output destination. By default, Slim’s logger will
write logged messages to <code>STDERR</code>. If you use a custom log writer, it must implement this interface:</p>
<pre><code>public write(mixed $message, int $level);
</code></pre>
<p>The <code>write()</code> method is responsible for sending the logged message (not necessarily a string) to the appropriate output
destination (e.g. a text file, a database, or a remote web service).</p>
<p>To specify a custom log writer after instantiation you must access Slim’s logger directly and use its <code>setWriter()</code> method:</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'log.writer' => new \My\LogWriter()
));
// After instantiation
$log = $app->getLog();
$log->setWriter(new \My\LogWriter());
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>mixed</dd>
<dt>Default Value</dt>
<dd>\Slim\LogWriter</dd>
</dl>
<h3>log.level</h3>
<div class="alert alert-info">
<strong>Heads Up!</strong> Use the constants defined in `\Slim\Log` instead of integers.
</div>
<p>Slim has five (5) log message levels:</p>
<ul>
<li>\Slim\Log::DEBUG</li>
<li>\Slim\Log::INFO</li>
<li>\Slim\Log::WARN</li>
<li>\Slim\Log::ERROR</li>
<li>\Slim\Log::FATAL</li>
</ul>
<p>The <code>log.level</code> application setting determines which logged messages will be honored and which will be ignored.
For example, if the <code>log.level</code> setting is <code>\Slim\Log::INFO</code>, debug messages will be ignored while info, warn,
error, and fatal messages will be logged.</p>
<p>To change this setting after instantiation you must access Slim’s logger directly and use its <code>setLevel()</code> method.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'log.level' => \Slim\Log::DEBUG
));
// After instantiation
$log = $app->getLog();
$log->setLevel(\Slim\Log::WARN);
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>integer</dd>
<dt>Default Value</dt>
<dd>\Slim\Log::DEBUG</dd>
</dl>
<h3>log.enabled</h3>
<p>This enables or disables Slim’s logger. To change this setting after instantiation you need to access Slim’s logger
directly and use its <code>setEnabled()</code> method.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'log.enabled' => true
));
// After instantiation
$log = $app->getLog();
$log->setEnabled(true);
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>boolean</dd>
<dt>Default Value</dt>
<dd>true</dd>
</dl>
<h3>templates.path</h3>
<p>The relative or absolute path to the filesystem directory that contains your Slim application’s template files.
This path is referenced by the Slim application’s View to fetch and render templates.</p>
<p>To change this setting after instantiation you need to access Slim’s view directly and use its <code>setTemplatesDirectory()</code>
method.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'templates.path' => './templates'
));
// After instantiation
$view = $app->view();
$view->setTemplatesDirectory('./templates');
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string</dd>
<dt>Default Value</dt>
<dd>”./templates”</dd>
</dl>
<h3>view</h3>
<p>The View class or instance used by the Slim application. To change this setting after instantiation you need to
use the Slim application’s <code>view()</code> method.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'view' => new \My\View()
));
// After instantiation
$app->view(new \My\View());
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string|\Slim\View</dd>
<dt>Default Value</dt>
<dd>\Slim\View</dd>
</dl>
<h3>cookies.lifetime</h3>
<p>Determines the lifetime of HTTP cookies created by the Slim application. If this is an integer, it must be a valid
UNIX timestamp at which the cookie expires. If this is a string, it is parsed by the <code>strtotime()</code> function to extrapolate
a valid UNIX timestamp at which the cookie expires.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.lifetime' => '20 minutes'
));
// After instantiation
$app->config('cookies.lifetime', '20 minutes');
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>integer|string</dd>
<dt>Default Value</dt>
<dd>“20 minutes”</dd>
</dl>
<h3>cookies.path</h3>
<p>Determines the default HTTP cookie path if none is specified when invoking the Slim application’s <code>setCookie()</code> or
<code>setEncryptedCookie()</code> methods.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.path' => '/'
));
// After instantiation
$app->config('cookies.lifetime', '/');
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string</dd>
<dt>Default Value</dt>
<dd>”/”</dd>
</dl>
<h3>cookies.domain</h3>
<p>Determines the default HTTP cookie domain if none specified when invoking the Slim application’s <code>setCookie()</code> or
<code>setEncryptedCookie()</code> methods.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.domain' => 'domain.com'
));
// After instantiation
$app->config('cookies.domain', 'domain.com');
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string</dd>
<dt>Default Value</dt>
<dd>null</dd>
</dl>
<h3>cookies.secure</h3>
<p>Determines whether or not cookies are delivered only via HTTPS. You may override this setting when invoking
the Slim application’s <code>setCookie()</code> or <code>setEncryptedCookie()</code> methods.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.secure' => false
));
// After instantiation
$app->config('cookies.secure', false);
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>boolean</dd>
<dt>Default Value</dt>
<dd>false</dd>
</dl>
<h3>cookies.httponly</h3>
<p>Determines whether or not cookies are delivered only via the HTTP protocol. You may override this setting when invoking
the Slim application’s <code>setCookie()</code> or <code>setEncryptedCookie()</code> methods.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.httponly' => false
));
// After instantiation
$app->config('cookies.httponly', false);
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>boolean</dd>
<dt>Default Value</dt>
<dd>false</dd>
</dl>
<h3>cookies.secret_key</h3>
<p>The secret key used for cookie encryption. You should change this setting if you use encrypted HTTP cookies
in your Slim application.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.secret_key' => 'secret'
));
// After instantiation
$app->config('cookies.secret_key', 'secret');
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string</dd>
<dt>Default Value</dt>
<dd>“CHANGE_ME”</dd>
</dl>
<h3>cookies.cipher</h3>
<p>The mcrypt cipher used for HTTP cookie encryption. See <a href="http://php.net/manual/en/mcrypt.ciphers.php">available ciphers</a>.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.cipher' => MCRYPT_RIJNDAEL_256
));
// After instantiation
$app->config('cookies.cipher', MCRYPT_RIJNDAEL_256);
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>integer</dd>
<dt>Default Value</dt>
<dd>MCRYPT_RIJNDAEL_256</dd>
</dl>
<h3>cookies.cipher_mode</h3>
<p>The mcrypt cipher mode used for HTTP cookie encryption. See <a href="http://php.net/manual/en/mcrypt.ciphers.php">available cipher modes</a>.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'cookies.cipher_mode' => MCRYPT_MODE_CBC
));
// After instantiation
$app->config('cookies.cipher_mode', MCRYPT_MODE_CBC);
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>integer</dd>
<dt>Default Value</dt>
<dd>MCRYPT_MODE_CBC</dd>
</dl>
<h3>http.version</h3>
<p>By default, Slim returns an HTTP/1.1 response to the client. Use this setting if you need to return an HTTP/1.0
response. This is useful if you use PHPFog or an nginx server configuration where you communicate with backend
proxies rather than directly with the HTTP client.</p>
<pre><code><?php
// During instantiation
$app = new \Slim\Slim(array(
'http.version' => '1.1'
));
// After instantiation
$app->config('http.version', '1.1');
</code></pre>
<dl>
<dt>Data Type</dt>
<dd>string</dd>
<dt>Default Value</dt>
<dd>“1.1”</dd>
</dl>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="Application-Names-and-Scopes">
<header>
<h2 class="page-article-header">Application Names and Scopes</h2>
</header>
<p>When you build a Slim application you will enter various scopes in your code (e.g. global scope and function scope).
You will likely need a reference to your Slim application in each scope. There are several ways to do this:</p>
<ul>
<li>Use application names with the Slim application’s <code>getInstance()</code> static method</li>
<li>Curry an application instance into function scope with the <code>use</code> keyword</li>
</ul>
<h3>Application Names</h3>
<p>Every Slim application may be given a name. <strong>This is optional</strong>. Names help you get a reference to a Slim
application instance in any scope throughout your code. Here is how you set and get an application’s name:</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->setName('foo');
$name = $app->getName(); // "foo"
</code></pre>
<h3>Scope Resolution</h3>
<p>So how do you get a reference to your Slim application? The example below demonstrates how to obtain a reference
to a Slim application within a route callback function. The <code>$app</code> variable is used in the global scope to define
the HTTP GET route. But the <code>$app</code> variable is also needed within the route’s callback scope to render a template.</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->get('/foo', function () {
$app->render('foo.php'); // <-- ERROR
});
</code></pre>
<p>This example fails because the <code>$app</code> variable is unavailable inside the route callback function.</p>
<h4>Currying</h4>
<p>We can inject the <code>$app</code> variable into the callback function with the <code>use</code> keyword:</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->get('/foo', function () use ($app) {
$app->render('foo.php'); // <-- SUCCESS
});
</code></pre>
<h4>Fetch by Name</h4>
<p>You can use the Slim application’s <code>getInstance()</code> static method, too:</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->get('/foo', 'foo');
function foo() {
$app = Slim::getInstance();
$app->render('foo.php');
}
</code></pre>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="Application-Modes">
<header>
<h2 class="page-article-header">Application Modes</h2>
</header>
<p>It is common practice to run web applications in a specific mode depending on the current state of the project.
If you are developing the application, you will run the application in “development” mode; if you are testing the
application, you will run the application in “test” mode; if you launch the application, you will run the application
in “production” mode.</p>
<p>Slim supports the concept of modes in that you may define your own modes and prompt Slim to prepare itself
appropriately for the current mode. For example, you may want to enable debugging in “development” mode but not
in “production” mode. The examples below demonstrate how to configure Slim differently for a given mode.</p>
<h3>What is a mode?</h3>
<p>Technically, an application mode is merely a string of text - like “development” or “production” - that has an
associated callback function used to prepare the Slim application appropriately. The application mode may be
anything you like: “testing”, “production”, “development”, or even “foo”.</p>
<h3>How do I set the application mode?</h3>
<div class="alert alert-info">
<strong>Heads Up!</strong> The application mode may only be set during application instantiation. It may
not be changed afterward.
</div>
<h4>Use an environment variable</h4>
<p>If Slim sees an environment variable named “SLIM_MODE”, it will set the application mode to that variable’s value.</p>
<pre><code><?php
$_ENV['SLIM_MODE'] = 'production';
</code></pre>
<h4>Use application setting</h4>
<p>If an environment variable is not found, Slim will next look for the mode in the application settings.</p>
<pre><code><?php
$app = new \Slim\Slim(array(
'mode' => 'production'
));
</code></pre>
<h4>Default mode</h4>
<p>If the environment variable and application setting are not found, Slim will set the application mode to “development”.</p>
<h3>Configure for a Specific Mode</h3>
<p>After you instantiate a Slim application, you may configure the Slim application for a specific mode
with the Slim application’s <code>configureMode()</code> method. This method accepts two arguments: the name of the target mode
and a callable function to be immediately invoked if the first argument matches the current application mode.</p>
<p>Assume the current application mode is “production”. Only the callable associated with the “production” mode will
be invoked. The callable associated with the “development” mode will be ignored until the application mode is
changed to “development”.</p>
<pre><code><?php
// Set the current mode
$app = new \Slim\Slim(array(
'mode' => 'production'
));
// Only invoked if mode is "production"
$app->configureMode('production', function () use ($app) {
$app->config(array(
'log.enable' => true,
'debug' => false
));
});
// Only invoked if mode is "development"
$app->configureMode('development', function () use ($app) {
$app->config(array(
'log.enable' => false,
'debug' => true
));
});
</code></pre>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
</section>
<section class="page-section" id="Routing">
<h1 class="page-header">Routing <a class="bookmark" href="http://docs.slimframework.com/#Routing"><i class="icon-bookmark"></i></a></h1>
<article class="page-article" id="Routing-Overview">
<header>
<h2 class="page-article-header">Routing Overview</h2>
</header>
<p>The Slim Framework helps you map resource URIs to callback functions for specific HTTP request methods
(e.g. GET, POST, PUT, DELETE, OPTIONS or HEAD). A Slim application will invoke the first route that matches the
current HTTP request’s URI and method.</p>
<p>If the Slim application does not find routes with URIs that match the HTTP request URI and method, it will
automatically return a <strong>404 Not Found</strong> response.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="GET-Routes">
<header>
<h2 class="page-article-header">GET Routes</h2>
</header>
<p>Use the Slim application’s <code>get()</code> method to map a callback function to a resource URI that is requested with
the HTTP GET method.</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->get('/books/:id', function ($id) {
//Show book identified by $id
});
</code></pre>
<p>In this example, an HTTP GET request for “/books/1” will invoke the associated callback function, passing “1” as the
callback’s argument.</p>
<p>The first argument of the Slim application’s <code>get()</code> method is the resource URI. The last argument is anything that
returns <code>true</code> for <code>is_callable()</code>. Typically, the last argument will be an <a href="http://php.net/manual/en/functions.anonymous.php">anonymous function</a>.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="POST-Routes">
<header>
<h2 class="page-article-header">POST Routes</h2>
</header>
<p>Use the Slim application’s <code>post()</code> method to map a callback function to a resource URI that is requested with
the HTTP POST method.</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->post('/books', function () {
//Create book
});
</code></pre>
<p>In this example, an HTTP POST request for “/books” will invoke the associated callback function</p>
<p>The first argument of the Slim application’s <code>post()</code> method is the resource URI. The last argument is anything that
returns <code>true</code> for <code>is_callable()</code>. Typically, the last argument will be an <a href="http://php.net/manual/en/functions.anonymous.php">anonymous function</a>.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="PUT-Routes">
<header>
<h2 class="page-article-header">PUT Routes</h2>
</header>
<p>Use the Slim application’s <code>put()</code> method to map a callback function to a resource URI that is requested with
the HTTP PUT method.</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->put('/books/:id', function ($id) {
//Update book identified by $id
});
</code></pre>
<p>In this example, an HTTP PUT request for “/books/1” will invoke the associated callback function, passing “1” as
the callback function’s argument.</p>
<p>The first argument of the Slim application’s <code>put()</code> method is the resource URI. The last argument is anything that
returns <code>true</code> for <code>is_callable()</code>. Typically, the last argument will be an <a href="http://php.net/manual/en/functions.anonymous.php">anonymous function</a>.</p>
<h3>Method Override</h3>
<p>Unfortunately, modern browsers do not provide native support for HTTP PUT requests. To work around this limitation,
ensure your HTML form’s method attribute is “post”, then add a method override parameter to your HTML form like this:</p>
<pre><code><form action="/books/1" method="post">
... other form fields here...
<input type="hidden" name="_METHOD" value="PUT"/>
<input type="submit" value="Update Book"/>
</form>
</code></pre>
<p>If you are using <a href="http://documentcloud.github.com/backbone/">Backbone.js</a> or a command-line HTTP client, you may also override the HTTP method by
using the <strong>X-HTTP-Method-Override</strong> header.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="DELETE-Routes">
<header>
<h2 class="page-article-header">DELETE Routes</h2>
</header>
<p>Use the Slim application’s <code>delete()</code> method to map a callback function to a resource URI that is requested with
the HTTP DELETE method.</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->delete('/books/:id', function ($id) {
//Delete book identified by $id
});
</code></pre>
<p>In this example, an HTTP DELETE request for “/books/1” will invoke the associated callback function, passing “1” as
the callback function’s argument.</p>
<p>The first argument of the Slim application’s <code>delete()</code> method is the resource URI. The last argument is anything that
returns <code>true</code> for <code>is_callable()</code>. Typically, the last argument will be an <a href="http://php.net/manual/en/functions.anonymous.php">anonymous function</a>.</p>
<h3>Method Override</h3>
<p>Unfortunately, modern browsers do not provide native support for HTTP DELETE requests. To work around this limitation,
ensure your HTML form’s method attribute is “post”, then add a method override parameter to your HTML form like this:</p>
<pre><code><form action="/books/1" method="post">
... other form fields here...
<input type="hidden" name="_METHOD" value="DELETE"/>
<input type="submit" value="Delete Book"/>
</form>
</code></pre>
<p>If you are using <a href="http://documentcloud.github.com/backbone/">Backbone.js</a> or a command-line HTTP client, you may also override the HTTP method by
using the <strong>X-HTTP-Method-Override</strong> header.</p>
<div class="page-article-footer">
<a class="btn" href="#top">Back to Top <i class="icon-arrow-up"></i></a>
</div>
</article>
<article class="page-article" id="OPTIONS-Routes">
<header>
<h2 class="page-article-header">OPTIONS Routes</h2>
</header>
<p>Use the Slim application’s <code>options()</code> method to map a callback function to a resource URI that is requested with
the HTTP OPTIONS method.</p>
<pre><code><?php
$app = new \Slim\Slim();
$app->options('/books/:id', function ($id) {
//Return response headers
});
</code></pre>
<p>In this example, an HTTP OPTIONS request for “/books/1” will invoke the associated callback function, passing “1” as
the callback function’s argument.</p>
<p>The first argument of the Slim application’s <code>options()</code> method is the resource URI. The last argument is anything that
returns <code>true</code> for <code>is_callable()</code>. Typically, the last argument will be an <a href="http://php.net/manual/en/functions.anonymous.php">anonymous function</a>.</p>
<h3>Method Override</h3>
<p>Unfortunately, modern browsers do not provide native support for HTTP OPTIONS requests. To work around this limitation,
ensure your HTML form’s method attribute is “post”, then add a method override parameter to your HTML form like this:</p>
<pre><code><form action="/books/1" method="post">
... other form fields here...
<input type="hidden" name="_METHOD" value="OPTIONS"/>
<input type="submit" value="Fetch Options For Book"/>
</form>