19 |
20 |
--------------------------------------------------------------------------------
/docs/recipes/file/is-directory.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining if a Path is a Directory
3 | Topics: file system
4 | Code: File::isDirectory(), is_dir()
5 | Id: 140
6 | Position: 16
7 | ---
8 |
9 | {problem}
10 | You want to check if a filepath is a directory.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::isDirectory()` method.
15 |
16 | {php}
17 | if (File::isDirectory($filename))
18 | {
19 | echo "Yes. It's a directory.";
20 | }
21 | {/php}
22 |
23 | If the path exists and is a directory `true` is returned. Otherwise `false` is returned.
24 | {/solution}
25 |
26 | {discussion}
27 | This is a simple wrapper on the PHP `is_dir()` function.
28 | {/discussion}
29 |
--------------------------------------------------------------------------------
/docs/recipes/form/set-session.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Session Store Implementation
3 | Topics: forms
4 | Code: Form::setSessionStore()
5 | Id: 179
6 | Position: 28
7 | ---
8 |
9 | {problem}
10 | You want to change the form's session store implementation.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Form::setSessionStore()` method.
15 |
16 | {php}
17 | $session = new MySessionHandler;
18 | Form::setSessionStore($session);
19 | {/php}
20 | {/solution}
21 |
22 | {discussion}
23 | This is most useful for testing.
24 |
25 | If you're unit testing a form macro you've created, this method allows you to mock the session store implementation as needed.
26 | {/discussion}
27 |
--------------------------------------------------------------------------------
/docs/recipes/install/memcached.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing Memcached
3 | Topics: cache, installation, memcached
4 | Code: -
5 | Id: 93
6 | Position: 11
7 | ---
8 |
9 | {problem}
10 | You want to speed up your application using a cache.
11 | {/problem}
12 |
13 | {solution}
14 | Install Memcached
15 |
16 | {bash}
17 | $ sudo apt-get install -y memcached php5-memcached
18 | $ sudo service apache2 restart
19 | {/bash}
20 |
21 | The first line installs the package, the second restarts apache.
22 | {/solution}
23 |
24 | {discussion}
25 | Now you can configure your cache to use memcached.
26 |
27 | See See [[Setting up the Memcached Cache Driver]] for instructions.
28 | {/discussion}
29 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_063522_create_session_table.php:
--------------------------------------------------------------------------------
1 | string('id')->unique();
15 | $t->text('payload');
16 | $t->integer('last_activity');
17 | $t->engine = 'MyISAM';
18 | });
19 | }
20 |
21 | /**
22 | * Reverse the migrations.
23 | */
24 | public function down()
25 | {
26 | Schema::drop('sessions');
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/docs/recipes/cache/forever.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Storing an Item in the Cache Forever
3 | Topics: cache
4 | Code: Cache::flush(), Cache::forever()
5 | Id: 269
6 | Position: 18
7 | ---
8 |
9 | {problem}
10 | You want to store an item in the cache indefintely.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Cache::forever()` method.
15 |
16 | This will permanently store the item.
17 |
18 | {php}
19 | Cache::forever($key, $value);
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | Clearing the cache will remove permanent items.
25 |
26 | If you issue `artisan cache:clear` or call `Cache::flush()` then all items, including those stored with `Cache::forever()` will be removed.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/form/get-session.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Session Store
3 | Topics: forms
4 | Code: Form::getSessionStore()
5 | Id: 178
6 | Position: 27
7 | ---
8 |
9 | {problem}
10 | You want to access the session implementation used by the `Form` facade.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Form::getSessionStore()` method.
15 |
16 | {php}
17 | $session = Form::getSessionStore();
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Generally this is not needed.
23 |
24 | You can access the `Session` facade directly. But if you have a complicated application with different session handlers for different parts of your application, Laravel provides this method.
25 | {/discussion}
26 |
--------------------------------------------------------------------------------
/docs/recipes/lang/event-changed.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Listening for Locale Changes
3 | Topics: -
4 | Code: App::setLocale(), Event::listen()
5 | Id: 271
6 | Position: 10
7 | ---
8 |
9 | {problem}
10 | You want to listen for locale changes within your application.
11 | {/problem}
12 |
13 | {solution}
14 | Listen for the `locale.changed` event.
15 |
16 | When fired, the event passes the new locale.
17 |
18 | {php}
19 | Event::listen('locale.changed', function($locale)
20 | {
21 | echo "Locale changed to ", $locale;
22 | });
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | This event is fired when `App::setLocale()` is called.
28 |
29 | See [[Setting the Default Locale]].
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/auth/password-expire.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Changing the Password Reminder Expiration
3 | Topics: authentication, configuration
4 | Code: -
5 | Id: 77
6 | Position: 11
7 | ---
8 |
9 | {problem}
10 | You don't like the default password expiration of one hour.
11 | {/problem}
12 |
13 | {solution}
14 | Edit your `app/config/auth.php` file.
15 |
16 | {php}
17 | 'reminder' => array(
18 | 'expire' => 60,
19 | ),
20 | {/php}
21 |
22 | Change the value of 60 to your desired expiration.
23 | {/solution}
24 |
25 | {discussion}
26 | The expiration time is a security feature.
27 |
28 | This keeps tokens sent your users short-lived so there's less time for hackers to guess.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/cache/increment.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Incrementing the Value of an Item in the Cache
3 | Topics: cache
4 | Code: Config::increment()
5 | Id: 273
6 | Position: 21
7 | ---
8 |
9 | {problem}
10 | You want to increment a value in the cache.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Cache::increment()`
15 |
16 | {php}
17 | $value = Cache::increment('key');
18 | {/php}
19 |
20 | The first time this is called `$value` will be 1, the next call it will return 2, and so forth.
21 |
22 | You can also pass a value to increment.
23 |
24 | {php}
25 | $value = Cache::increment('key', 50);
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | Doesn't work with File or Database caches.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/app/shutdown-cb.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering a Shutdown Callback
3 | Topics: callbacks
4 | Code: App::shutdown()
5 | Id: 55
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to execute code just before your application finishes.
11 | {/problem}
12 |
13 | {solution}
14 | Register a Shutdown callback.
15 |
16 | {php}
17 | App::shutdown(function()
18 | {
19 | // gets called right before app quits.
20 | });
21 | {/php}
22 | {/solution}
23 |
24 | {discussion}
25 | Shutdown callbacks occur after the response has been sent to the user.
26 |
27 | They occur during the shutdown process, right before the application exits. Typically they're used to close open services or do logging.
28 | {/discussion}
29 |
--------------------------------------------------------------------------------
/docs/recipes/file/exists.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining If a File Exists
3 | Topics: file system
4 | Code: File::exists()
5 | Id: 123
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to see if a file exists.
11 |
12 | You know you can use the PHP `file_exists()` method, but want to do it the Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `File::exists()` method.
17 |
18 | {php}
19 | if (File::exists($myfile))
20 | {
21 | echo "Yup. It exists.";
22 | }
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | This method actually calls `file_exists()`.
28 |
29 | But it does allow better testing because using the facade allows you to easily mock the method when needed.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/auth-clear.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Clearing Expired Password Reminders
3 | Topics: artisan, authentication, password reminders
4 | Code: -
5 | Id: 72
6 | Position: 20
7 | ---
8 |
9 | {problem}
10 | Your password reminders table is getting large.
11 |
12 | Lots of records exist in the table.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan auth:clear-reminders` command.
17 |
18 | This will clear out any expired tokens from the `password_reminders` table.
19 |
20 | {php}
21 | $ php artisan auth:clear-reminders
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | Run this command periodically.
27 |
28 | You may even want to have a cron job run this once a day or once a week.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/up.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Bringing the Application Out of Maintenance Mode
3 | Topics: artisan
4 | Code: -
5 | Id: 62
6 | Position: 10
7 | ---
8 |
9 | {problem}
10 | You need to bring your application out of "maintenance" mode.
11 |
12 | You've been in maintenance mode and are ready to make your application live.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan up` command.
17 |
18 | This clears a flag on your application specifying it's no longer in maintenance mode.
19 |
20 | {php}
21 | $ php artisan up
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | This command deletes a file.
27 |
28 | What this command actually does is delete the file `app/storage/meta/down`.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/view-pub.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Publishing a Package's Views to Your Application
3 | Topics: artisan
4 | Code: -
5 | Id: 278
6 | Position: 25
7 | ---
8 |
9 | {problem}
10 | You want to copy a third party's views to your application.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan view:publish` command.
15 |
16 | {bash}
17 | $ php artisan view:publish cool-package
18 | {/bash}
19 |
20 | This creates all needed configuration files in your `app/views/cool-package` directory.
21 | {/solution}
22 |
23 | {discussion}
24 | For non-Laravel packages you may need to specify the path.
25 |
26 | {bash}
27 | $ php artisan view:publish cool-package --path=/package/view/dir
28 | {/bash}
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/auth/get-request.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Authentication Request Instance
3 | Topics: -
4 | Code: Auth::getRequest()
5 | Id: 233
6 | Position: 38
7 | ---
8 |
9 | {problem}
10 | You want to access the request instance used by authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::getRequest()` method.
15 |
16 | {php}
17 | $request = Auth::getRequest();
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Usually you can just access the `Request` facade.
23 |
24 | By default Laravel uses the same request for both the `Request` facade and the `Auth` facade. This means unless your application is explicitly setting the request, it's easier to use the `Request` facade.
25 | {/discussion}
26 |
--------------------------------------------------------------------------------
/docs/recipes/form/token.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Generating a Hidden Field With the CSRF Token
3 | Topics: forms
4 | Code: Form::model(), Form::open(), Form::token()
5 | Id: 153
6 | Position: 4
7 | ---
8 |
9 | {problem}
10 | You want to output the CSRF token yourself.
11 |
12 | You know Laravel does this automatically with `Form::open()` or `Form::model()` but you want to do it yourself.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Form::token()` method.
17 |
18 | {html}
19 | {{ Form::token() }}
20 | {/html}
21 |
22 | This will output something like.
23 |
24 | {html}
25 |
26 | {/html}
27 | {/solution}
28 |
29 | {discussion}
30 | Nothing to discuss.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/form/close.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Closing the Current Form
3 | Topics: forms
4 | Code: Form::close()
5 | Id: 152
6 | Position: 3
7 | ---
8 |
9 | {problem}
10 | You want to close the current form.
11 |
12 | You know you can simply use a `` but want to do it the Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Form::close()` method.
17 |
18 | This is usually done in a Blade template.
19 |
20 | {html}
21 | {{ Form::close() }}
22 | {/html}
23 | {/solution}
24 |
25 | {discussion}
26 | This method actually does more than just return a ``.
27 |
28 | It also resets the `Form` internals (any defined labels and bound model). This is important if your web page has more than one form.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/cache/has.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining if an Item Exists in the Cache
3 | Topics: cache
4 | Code: Config::has()
5 | Id: 102
6 | Position: 10
7 | ---
8 |
9 | {problem}
10 | You want to see if a cached value exists.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Cache::has()`
15 |
16 | {php}
17 | if (Cache::has('mykey'))
18 | {
19 | echo "Yea! 'mykey' is cached!";
20 | }
21 | {/php}
22 | {/solution}
23 |
24 | {discussion}
25 | Doesn't work with null items.
26 |
27 | If you cache a null value, the `Cache::has()` method returns false.
28 |
29 | {php}
30 | Cache::put('test-null', null, 10);
31 | if ( ! Cache::has('test-null'))
32 | {
33 | echo "This line will always be output.";
34 | }
35 | {/php}
36 | {/discussion}
37 |
--------------------------------------------------------------------------------
/docs/recipes/auth/get-session.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Session Store Used for Authentication
3 | Topics: -
4 | Code: Auth::getSession()
5 | Id: 237
6 | Position: 42
7 | ---
8 |
9 | {problem}
10 | You want to access the session store used by authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::getSession()` method.
15 |
16 | {php}
17 | $request = Auth::getSession();
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Usually you can just access the `Session` facade.
23 |
24 | By default Laravel uses the same session store for both the `Session` facade and the `Auth` facade. This means unless your application is explicitly setting the session store, it's easier to use the `Session` facade.
25 | {/discussion}
26 |
--------------------------------------------------------------------------------
/docs/recipes/cache/decrement.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Decrementing the Value of an Item in the Cache
3 | Topics: cache
4 | Code: Config::decrement()
5 | Id: 274
6 | Position: 22
7 | ---
8 |
9 | {problem}
10 | You want to decrement a value in the cache.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Cache::decrement()`
15 |
16 | {php}
17 | $value = Cache::decrement('key');
18 | {/php}
19 |
20 | If the value of `key` is 10, then the first time this is called `$value` will be 9, the next call it will return 8, and so forth.
21 |
22 | You can also pass a value to decrement.
23 |
24 | {php}
25 | $value = Cache::decrement('key', 50);
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | Doesn't work with File or Database caches.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/auth/get-dispatch.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Event Dispatcher for Authentication
3 | Topics: -
4 | Code: Auth::getDispatcher()
5 | Id: 231
6 | Position: 36
7 | ---
8 |
9 | {problem}
10 | You want to access the event dispatcher used by authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::getDispatcher()` method.
15 |
16 | {php}
17 | $events = Auth::getDispatcher();
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Usually you can just access the `Event` facade.
23 |
24 | By default Laravel uses the same event driver for both the `Event` facade and the `Auth` facade. This means unless your application is explicitly setting the event dispatcher, it's easier to use the `Event` facade.
25 | {/discussion}
26 |
--------------------------------------------------------------------------------
/app/Recipes/Code.php:
--------------------------------------------------------------------------------
1 | belongsToMany('Recipe');
8 | }
9 |
10 | /**
11 | * Given a list of code names, return the list of topic ids
12 | */
13 | public static function idsFromNames(array $names)
14 | {
15 | $ids = [];
16 | foreach ($names as $name)
17 | {
18 | $topic = static::whereName($name)->first();
19 | if ( ! $topic)
20 | {
21 | $topic = new Code;
22 | $topic->name = $name;
23 | $topic->save();
24 | }
25 | $ids[] = $topic->id;
26 | }
27 | return $ids;
28 | }
29 | }
--------------------------------------------------------------------------------
/app/Recipes/Topic.php:
--------------------------------------------------------------------------------
1 | belongsToMany('Recipe');
8 | }
9 |
10 | /**
11 | * Given a list of topic names, return the list of topic ids
12 | */
13 | public static function idsFromNames(array $names)
14 | {
15 | $ids = [];
16 | foreach ($names as $name)
17 | {
18 | $topic = static::whereName($name)->first();
19 | if ( ! $topic)
20 | {
21 | $topic = new Topic;
22 | $topic->name = $name;
23 | $topic->save();
24 | }
25 | $ids[] = $topic->id;
26 | }
27 | return $ids;
28 | }
29 | }
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_063633_create_cache_table.php:
--------------------------------------------------------------------------------
1 | string('key')->unique();
16 | $table->mediumtext('value');
17 | $table->integer('expiration');
18 | $table->engine = 'MyISAM';
19 | });
20 | }
21 |
22 | /**
23 | * Reverse the migrations.
24 | */
25 | public function down()
26 | {
27 | Schema::drop('cache');
28 | }
29 |
30 | }
--------------------------------------------------------------------------------
/docs/recipes/auth/once.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Logging a User In Without Sessions or Cookies
3 | Topics: -
4 | Code: Auth::once()
5 | Id: 215
6 | Position: 20
7 | ---
8 |
9 | {problem}
10 | You want to log a user in without using cookies or the session.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::once()` method.
15 |
16 | This method takes an array containing the user's credentials.
17 |
18 | {php}
19 | $logged_in = Auth::once(['username' => 'test', 'password' => 'test']);
20 | if ( ! $logged_in)
21 | {
22 | throw new Exception('not logged in');
23 | }
24 | {/php}
25 | {/solution}
26 |
27 | {discussion}
28 | The user will remain _"logged in"_ only for the current request.
29 |
30 | This is a handy method to use when testing.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/install/sqlite.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing SQLite
3 | Topics: installation, SQLite
4 | Code: -
5 | Id: 119
6 | Position: 14
7 | ---
8 |
9 | {problem}
10 | You'd like to use SQLite, but PHP isn't configured for it.
11 | {/problem}
12 |
13 | {solution}
14 | Install the PHP Driver for SQLite.
15 |
16 | {bash}
17 | $ sudo apt-get install -y php5-sqlite
18 | {/bash}
19 |
20 | Then restart apache.
21 |
22 | {bash}
23 | $ sudo service apache2 restart
24 | {/bash}
25 | {/solution}
26 |
27 | {discussion}
28 | SQLite requires no server.
29 |
30 | It is a self-contained SQL database engine. Most often SQLite is used for testing, but it can be useful for small applications.
31 |
32 | See also [[Setting up the SQLite Database Driver]].
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/blade/unless.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Using the @unless Control Structure in Blade
3 | Topics: Blade
4 | Code: @endunless, @unless
5 | Id: 85
6 | Position: 5
7 | ---
8 |
9 | {problem}
10 | You need conditional logic in a Blade template.
11 |
12 | You want to output html within the template when a condition is false. Yes, you know you can do `@if ( ! condition)`, but are curious if there's a more elegant way.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `@unless` control structure.
17 |
18 | {html}
19 | @unless ($age >= 18)
20 | You can't vote.
21 | @endunless
22 | {/html}
23 | {/solution}
24 |
25 | {discussion}
26 | Pretty simple.
27 |
28 | What Blade does internally is change your `@unless(condition)` into a `if ( ! condition)`.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/file/append.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Appending to a File
3 | Topics: file system
4 | Code: File::append()
5 | Id: 132
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to write content to the _end_ of a file.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::append()` method.
15 |
16 | {php}
17 | $bytesWritten = File::append($filename, $content);
18 | if ($bytesWritten === false)
19 | {
20 | die("Couldn't write to the file.");
21 | }
22 | {/php}
23 |
24 | This will add the content to the end of existing files. If the file doesn't exist then the entire contents will be `$content`.
25 | {/solution}
26 |
27 | {discussion}
28 | Watch the return value.
29 |
30 | If there's a problem writing to the file `false` is returned.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/auth/artisan.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Artisan Password Reminder Commands
3 | Topics: authentication, configuration
4 | Code: -
5 | Id: 79
6 | Position: 13
7 | ---
8 |
9 | {problem}
10 | You want to know which artisan commands deal with Laravel's password reminder features.
11 | {/problem}
12 |
13 | {solution}
14 | Here's the list.
15 |
16 | * `php artisan auth:reminders` - [[Creating a Migration for Password Reminders]]
17 | * `php artisan auth:clear-reminders` - [[Clearing Expired Password Reminders]]
18 | * `php artisan auth:reminders-controller` - [[Creating a Reminders Controller]]
19 |
20 | Of course, you could also just type `php artisan` and read through the list of commands.
21 | {/solution}
22 |
23 | {discussion}
24 | Nothing to discuss.
25 | {/discussion}
26 |
--------------------------------------------------------------------------------
/docs/recipes/file/prepend.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Prepending to a File
3 | Topics: file system
4 | Code: File::prepend()
5 | Id: 131
6 | Position: 7
7 | ---
8 |
9 | {problem}
10 | You want to write content to the _beginning_ of a file.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::prepend()` method.
15 |
16 | {php}
17 | $bytesWritten = File::prepend($filename, $content);
18 | if ($bytesWritten === false)
19 | {
20 | die("Couldn't write to the file.");
21 | }
22 | {/php}
23 |
24 | This will add the content to the beginning of existing files. If the file doesn't exist then the entire contents will be `$content`.
25 | {/solution}
26 |
27 | {discussion}
28 | Watch the return value.
29 |
30 | If there's a problem writing to the file `false` is returned.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/auth/validate.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Validating a User's Credentials
3 | Topics: -
4 | Code: Auth::validate()
5 | Id: 216
6 | Position: 21
7 | ---
8 |
9 | {problem}
10 | You want to validate a user's credentials, but you don't want to log the user in.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::validate()` method.
15 |
16 | The method takes an array containing the user's credentials.
17 |
18 | {php}
19 | $credentials = [
20 | 'username' => 'mylogin',
21 | 'password' => 'mypass',
22 | ];
23 | $valid = Auth::validate($credentials);
24 | if ( ! $valid)
25 | {
26 | throw new Exception('Invalid credentials');
27 | }
28 | {/php}
29 | {/solution}
30 |
31 | {discussion}
32 | The validation will fire an `auth.attempt` event with the credentials.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/file/last-modified.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting a File's Last Modification Time
3 | Topics: file system
4 | Code: File::lastModified()
5 | Id: 139
6 | Position: 15
7 | ---
8 |
9 | {problem}
10 | You need to know the last time a file was modified.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::lastModified()` method.
15 |
16 | {php}
17 | $timestamp = File::lastModified($filename);
18 | if ($timestamp === false)
19 | {
20 | die("Failure getting the time");
21 | }
22 | {/php}
23 |
24 | The value returned is a Unix timestamp.
25 | {/solution}
26 |
27 | {discussion}
28 | If there's an error, a warning message is emitted.
29 |
30 | It's a good idea to make sure the file exists before getting the modification time. See [[Determining If a File Exists]].
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/app/lang/en/reminders.php:
--------------------------------------------------------------------------------
1 | "Passwords must be at least six characters and match the confirmation.",
17 |
18 | "user" => "We can't find a user with that e-mail address.",
19 |
20 | "token" => "This password reset token is invalid.",
21 |
22 | "sent" => "Password reminder sent!",
23 |
24 | );
25 |
--------------------------------------------------------------------------------
/docs/recipes/app/in-console.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Checking if You're Running in the Console
3 | Topics: console, environment
4 | Code: App::runningInConsole(), php_sapi_name()
5 | Id: 2
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You want to check if your application is running in the console.
11 |
12 | You know you can check `php_sapi_name()` but would like to use the more elegant, Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use `App::runningInConsole()`
17 |
18 | {php}
19 | if (App::runningInConsole())
20 | {
21 | echo "I'm in the console, baby!";
22 | }
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | Laravel actually uses `php_sapi_name()` to implement this method.
28 |
29 | If `php_sapi_name()` equals `'cli'` then your code is running in the console.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/app/push-error.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering a "Last Chance" Error Handler
3 | Topics: -
4 | Code: App::error(), App::pushError()
5 | Id: 205
6 | Position: 23
7 | ---
8 |
9 | {problem}
10 | You want to handle errors _ONLY_ if nothing else handles them.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::pushError()` method.
15 |
16 | This will add your handler to the bottom of the stack instead of the top of the stack.
17 |
18 | {php}
19 | App::pushError(function($exception)
20 | {
21 | die('ERROR: '.$exception->getMessage());
22 | });
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | This is exactly like `App::error()`.
28 |
29 | Except the handler is placed on the bottom of the stack instead of the top. See [[Registering an Error Handler]].
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/lang/set-locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Default Locale
3 | Topics: localization
4 | Code: Lang::setLocale()
5 | Id: 254
6 | Position: 5
7 | ---
8 |
9 | {problem}
10 | You want to change the default locale.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Lang::setLocale()` method.
15 |
16 | {php}
17 | Lang::setLocale('es'); // switch to Spanish
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Difference between `App::setLocale()` and `Lang::setLocale()`.
23 |
24 | `Lang::setLocale()` only changes the locale in the currently loaded translation service.
25 |
26 | `App::setLocale()` changes the currently loaded configuration value and calls `Lang::setLocale()` to update the translation service. Also, `App::setLocale()` fires a `locale.changed` event.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/session.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating a Migration for Sessions
3 | Topics: artisan
4 | Code: -
5 | Id: 70
6 | Position: 18
7 | ---
8 |
9 | {problem}
10 | You need to create the table to store database sessions.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan session:table` command.
15 |
16 | {php}
17 | $ php artisan session:table
18 | {/php}
19 |
20 | Note that this only creates the migration, you'll still need to run the migration.
21 |
22 | {php}
23 | $ php artisan migrate
24 | {/php}
25 | {/solution}
26 |
27 | {discussion}
28 | This is only needed if you use database sessions.
29 |
30 | If you change the driver in `app/config/session.php` to `database`, then you'll need to create the table to store your sessions in. This command accomplishes that.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/key-gen.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Generating a New Application Key
3 | Topics: artisan
4 | Code: Hash::make()
5 | Id: 283
6 | Position: 30
7 | ---
8 |
9 | {problem}
10 | You want to set a new application key.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan key:generate` command.
15 |
16 | {bash}
17 | $ php artisan key:generate
18 | Application key [Idgz1PE3zO9iNc0E3oeH3CHDPX9MzZe3] set successfully.
19 | {/bash}
20 | {/solution}
21 |
22 | {discussion}
23 | You shouldn't need to do this.
24 |
25 | When you first create your Laravel application, `key:generate` is automatically called.
26 |
27 | So it should already be set for you.
28 |
29 | If you change it by executing the command again, be aware that passwords saved with `Hash::make()` will no longer be valid.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/config-pub.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Publishing a Package's Configuration to Your Application
3 | Topics: artisan
4 | Code: -
5 | Id: 277
6 | Position: 24
7 | ---
8 |
9 | {problem}
10 | You want to copy a third party's configuration to your application.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan config:publish` command.
15 |
16 | {bash}
17 | $ php artisan config:publish cool-package
18 | {/bash}
19 |
20 | This creates all needed configuration files in your `app/config/packages/cool-package` directory.
21 |
22 | Often, this is a single file named `config.php`.
23 | {/solution}
24 |
25 | {discussion}
26 | For non-Laravel packages you may need to specify the path.
27 |
28 | {bash}
29 | $ php artisan config:publish cool-package --path=/some/config/dir
30 | {/bash}
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/crypt/setkey.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Encryption Key
3 | Topics: encryption
4 | Code: Config::get(), Crypt::decrypt(), Crypt::encrypt(), Crypt::setKey()
5 | Id: 107
6 | Position: 3
7 | ---
8 |
9 | {problem}
10 | You want to set your own encryption key.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Crypt::setKey()`.
15 |
16 | {php}
17 | Crypt::setKey($key);
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | This key will be used for subsequent `Crypt::encrypt()` and `Crypt::decrypt()` calls.
23 |
24 | But only in the current request.
25 |
26 | By default Laravel automatically uses `Config::get('app.key')` as the key for encryption. If you change the key, any subsequent calls to `Crypt::decrypt()` will only only work for values that have been encrypted with the same key.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/db-seed.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Seeding Your Database
3 | Topics: artisan
4 | Code: -
5 | Id: 69
6 | Position: 17
7 | ---
8 |
9 | {problem}
10 | You want to seed your database.
11 |
12 | You've already written your database seeds and now you want to tell Laravel to seed the database.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan db:seed` command.
17 |
18 | {php}
19 | $ php artisan db:seed
20 | {/php}
21 |
22 | If you're using a different class than default for the root seeder, you can use the `--class` option.
23 |
24 | {php}
25 | $ php artisan db:seed --class=MyDatabaseSeeder
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | Seeding is usually done for testing.
31 |
32 | It's a great way to generate a standard set of records on your development machine.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_103141_create_code_table.php:
--------------------------------------------------------------------------------
1 | increments('id');
18 | $table->string('name')->unique();
19 | $table->timestamps();
20 | $table->engine = 'MyISAM';
21 | });
22 | }
23 |
24 | /**
25 | * Reverse the migrations.
26 | *
27 | * @return void
28 | */
29 | public function down()
30 | {
31 | Schema::drop('codes');
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/docs/recipes/install/nginx.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing Nginx
3 | Topics: installation, Nginx
4 | Code: -
5 | Id: 21
6 | Position: 7
7 | ---
8 |
9 | {problem}
10 | You have no web server installed.
11 |
12 | You'd like to install a web server for your Laravel application to use.
13 | {/problem}
14 |
15 | {solution}
16 | Install Nginx.
17 |
18 | Here are the instructions for Ubuntu 13.04.
19 |
20 | {bash}
21 | laravel:~$ sudo apt-get install -y php5-fpm nginx
22 | laravel:~$ sudo service nginx start
23 | {/bash}
24 | {/solution}
25 |
26 | {discussion}
27 | Nginx is pronounced "engine-x".
28 |
29 | Nginx is rapidly becoming the favorite web server for techies everywhere.
30 |
31 | It's fast, easy to configure, and doesn't use as much memory as Apache.
32 |
33 | See also [[Creating a Nginx VirtualHost]]
34 | {/discussion}
35 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_103126_create_topic_table.php:
--------------------------------------------------------------------------------
1 | increments('id');
18 | $table->string('name')->unique();
19 | $table->timestamps();
20 | $table->engine = 'MyISAM';
21 | });
22 | }
23 |
24 | /**
25 | * Reverse the migrations.
26 | *
27 | * @return void
28 | */
29 | public function down()
30 | {
31 | Schema::drop('topics');
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/docs/recipes/auth/remember.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determine if the User Was Authenticated Via the Remember Cookie
3 | Topics: authentication
4 | Code: Auth::viaRemember()
5 | Id: 83
6 | Position: 17
7 | ---
8 |
9 | {problem}
10 | You want to know how the user was authenticated.
11 |
12 | You know there's only two possibilities. Either they were already logged into the existing session or they were automatically logged in with the "remember me" cookie.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Auth::viaRemember()` method.
17 |
18 | {php}
19 | if (Auth::viaRemember())
20 | {
21 | echo "Aha, you logged in with the remember me cookie.";
22 | }
23 | else
24 | {
25 | echo "You were already logged in for this request.";
26 | }
27 | {/php}
28 | {/solution}
29 |
30 | {discussion}
31 | Nothing to discuss.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/docs/recipes/hash/make.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Hashing a Value
3 | Topics: -
4 | Code: Hash::make()
5 | Id: 125
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to create a hash of a string.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Hash::make()` method.
15 |
16 | {php}
17 | $hashed_password = Hash::make($plaintext_password);
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Hashing uses the Blowfish algorithm.
23 |
24 | See [The bcrypt Wikipedia article](http://en.wikipedia.org/wiki/Bcrypt).
25 |
26 | You can also specify the cost parameter for hashing. This is a base-2 logarithm of the times the hashing routine is iterated.
27 |
28 | {php}
29 | $hashed = Hash::make($plaintext, array('rounds' => 10));
30 | {/php}
31 |
32 | This will change the the cost parameter from the default of 8 to 10.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/auth/logout.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Logging the User Out of Your Application
3 | Topics: -
4 | Code: Auth::logout(), Event::listen(), Log::info()
5 | Id: 224
6 | Position: 29
7 | ---
8 |
9 | {problem}
10 | You want to log the user out.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::logout()` method.
15 |
16 | {php}
17 | Auth::logout();
18 | {/php}
19 |
20 | This will clear the user from memory and any session storage of the user will be cleared also.
21 | {/solution}
22 |
23 | {discussion}
24 | This files the `auth.logout` event.
25 |
26 | You can listen for it. Here's an example that logs when the user logs out.
27 |
28 | {php}
29 | Event::listen('auth.logout', function($user)
30 | {
31 | $message = sprintf('User #%d logged out', $user->id);
32 | Log::info($message);
33 | });
34 | {/php}
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/cache/wincache.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting up the WinCache Cache Driver
3 | Topics: cache, configuration
4 | Code: -
5 | Id: 98
6 | Position: 7
7 | ---
8 |
9 | {problem}
10 | You are running PHP in Windows and want to use the WinCache Driver.
11 | {/problem}
12 |
13 | {solution}
14 | Configure caching to use it.
15 |
16 | Edit `app/config/cache.php` and change the driver to `'wincache'`.
17 |
18 | {php}
19 | 'driver' => 'wincache',
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | WinCache requires the IIS server using the FastCGI extension.
25 |
26 | Detailed setup instructions are beyond the scope of this book. You can find out more at:
27 |
28 | * [PHP Documentation Page](http://www.php.net/manual/en/wincache.requirements.php)
29 | * [WinCache PECL Page](http://pecl.php.net/package/wincache)
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/install/apache.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing Apache
3 | Topics: Apache, installation
4 | Code: -
5 | Id: 20
6 | Position: 6
7 | ---
8 |
9 | {problem}
10 | You have no web server installed.
11 |
12 | You'd like to install a web server for your Laravel application to use.
13 | {/problem}
14 |
15 | {solution}
16 | Install Apache.
17 |
18 | {bash}
19 | laravel:~$ sudo apt-get install -y apache2 libapache2-mod-php5
20 | laravel:~$ sudo a2enmod rewrite
21 | laravel:~$ sudo service apache2 restart
22 | {/bash}
23 | {/solution}
24 |
25 | {discussion}
26 | Apache is the most dominant web server on the Internet.
27 |
28 | Notice the second line in the above instructions. This enables **mod-rewrite** with Apache. This is required for Laravel to work correctly.
29 |
30 | See also [[Creating an Apache VirtualHost]]
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/auth/once-id.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Logging in a User by ID Without Sessions or Cookies
3 | Topics: -
4 | Code: Auth::onceUsingId()
5 | Id: 223
6 | Position: 28
7 | ---
8 |
9 | {problem}
10 | You want to log a user in by the user id.
11 |
12 | But, you want them only available in the current request.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Auth::onceUsingId()` method.
17 |
18 | {php}
19 | $success = Auth::onceUsingId($user_id);
20 | if ( ! $success)
21 | {
22 | throw new Exception('failed!');
23 | }
24 | {/php}
25 | {/solution}
26 |
27 | {discussion}
28 | This is similar to `Auth::once()`.
29 |
30 | But it doesn't do the authentication. You pass the `$user_id` and if the id is valid, the user is logged in for the current session.
31 |
32 | See also [[Logging a User In Without Sessions or Cookies]].
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/public/assets/js/recipes.js:
--------------------------------------------------------------------------------
1 | /* Recipes JS */
2 |
3 |
4 | /**
5 | * Startup code
6 | */
7 | (function($) {
8 | $("#section-banner1").hover(
9 | function()
10 | {
11 | $("#l5beauty-img").slideDown(400);
12 | $("#buy-book-link1").html('Buy my book');
13 | },
14 | function()
15 | {
16 | $("#buy-book-link1").html('More...');
17 | $("#l5beauty-img").slideUp(300);
18 | }
19 | );
20 | $("#section-banner2").hover(
21 | function()
22 | {
23 | $("#gsd-laravel-img").slideDown(400);
24 | $("#buy-book-link2").html('Buy my book');
25 | },
26 | function()
27 | {
28 | $("#buy-book-link2").html('More...');
29 | $("#gsd-laravel-img").slideUp(300);
30 | }
31 | );
32 | })(jQuery);
--------------------------------------------------------------------------------
/docs/recipes/auth/get-cookie.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Cookie Jar Used for Authentication
3 | Topics: -
4 | Code: Auth::getCookieJar()
5 | Id: 225
6 | Position: 30
7 | ---
8 |
9 | {problem}
10 | You want to access the cookie creator used by authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::getCookieJar()` method.
15 |
16 | {php}
17 | $cookies = Auth::getCookieJar();
18 | {/php}
19 |
20 | Note if authentication isn't using cookies a `RuntimeException` will be thrown.
21 | {/solution}
22 |
23 | {discussion}
24 | Usually you can just access the `Cookie` facade.
25 |
26 | By default Laravel uses the same cookie driver for both the `Cookie` facade and the `Auth` facade. This means unless your application is explicitly setting the cookie jar, it's easier to use the `Cookie` facade when dealing with cookies.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/cache/prefix.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Using a Cache Key Prefix
3 | Topics: cache
4 | Code: -
5 | Id: 276
6 | Position: 24
7 | ---
8 |
9 | {problem}
10 | You have multiple applications using the same cache and want to differentiate keys between them.
11 | {/problem}
12 |
13 | {solution}
14 | Set an application specific cache prefix.
15 |
16 | Edit your `app/config/cache.php` file and change the `prefix` setting.
17 |
18 | {php}
19 | 'myprefix',
24 | );
25 | ?>
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | Every item retrieved or stored in the cache will automatically have this prefix.
31 |
32 | Note the File and Array cache drivers do not use the prefix. This is because those drivers are already considered application specific.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/file/directories.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting All the Directories in a Directory
3 | Topics: file system
4 | Code: File::directories()
5 | Id: 146
6 | Position: 22
7 | ---
8 |
9 | {problem}
10 | You want to get a list of all the directories with a directory.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::directories()` method.
15 |
16 | {php}
17 | $list = File::directories('/test');
18 | echo "Test contains ", count($list), "directories.";
19 | {/php}
20 | {/solution}
21 |
22 | {discussion}
23 | The method returns an array of strings.
24 |
25 | Each string is the full path to the subdirectories of the directory argument.
26 |
27 | If the directory used as the argument contains no subdirectories an empty array is returned.
28 |
29 | If the directory argument doesn't exist, an `InvalidArgumentException` will be thrown.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/cookie/get.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Retrieving a Cookie from the Request
3 | Topics: cookies
4 | Code: Cookie::get(), Request::cookie()
5 | Id: 49
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You want to check a cookie value sent to your application.
11 |
12 | You know you could use the PHP `$_COOKIE` superglobal, but want to do it the Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use `Cookie::get()`.
17 |
18 | {php}
19 | // $val will be null if cookie not present
20 | $val = Cookie::get('COOKIE_NAME');
21 |
22 | // Or you can pass a default, if not present
23 | $name = Cookie::get('NAME', 'Unknown');
24 | echo "Hello $name";
25 | {/php}
26 | {/solution}
27 |
28 | {discussion}
29 | This is basically the same as `Request::cookie()`.
30 |
31 | In fact, the `Cookie::get()` method is actually a wrapper over `Request::cookie()`.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/docs/recipes/blade/show.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Yielding the Current Section in a Blade Template
3 | Topics: -
4 | Code: @show
5 | Id: 240
6 | Position: 12
7 | ---
8 |
9 | {problem}
10 | You want to yield the contents of the section you're currently in.
11 | {/problem}
12 |
13 | {solution}
14 | Use the Blade `@show` command.
15 |
16 | This ends the current section and yields it. Yielding means this is the point the content of the section will be output.
17 |
18 | {html}
19 | @section('one')
20 | Something here
21 | But we'll end it, so it won't output
22 | @stop
23 |
24 | @section('two')
25 | Something else
26 | @show
27 | {/html}
28 |
29 | The HTML output from above would be.
30 |
31 | {html}
32 | Something else
33 | {/html}
34 | {/solution}
35 |
36 | {discussion}
37 | You can think of `@show` as a `@stop` followed by a `@yield('section')`.
38 | {/discussion}
39 |
--------------------------------------------------------------------------------
/docs/recipes/cache/put.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Storing an Item in the Cache
3 | Topics: cache
4 | Code: Cache::get(), Cache::put()
5 | Id: 266
6 | Position: 15
7 | ---
8 |
9 | {problem}
10 | You want to store a value in the cache.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Cache::put()` method.
15 |
16 | {php}
17 | Cache::put($key, $value, $minutes);
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | Cache storage and retrieval usually follows the pattern below.
23 |
24 | {php}
25 | // 1. Pull the item from cache
26 | $value = Cache::get($key);
27 |
28 | // 2. Check if it was not found
29 | if ($value === null)
30 | {
31 | // 3. Figure the value manually
32 | $value = 'just something easy';
33 |
34 | // 4. Store it for a number of minutes
35 | $minutes = 30;
36 | Cache::put($key, $value, $minutes);
37 | }
38 | {/php}
39 | {/discussion}
40 |
--------------------------------------------------------------------------------
/docs/recipes/app/after.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering an After Application Filter
3 | Topics: filters
4 | Code: App::after()
5 | Id: 54
6 | Position: 7
7 | ---
8 |
9 | {problem}
10 | You want to do work after every request in your application.
11 | {/problem}
12 |
13 | {solution}
14 | Register a "after" application filter.
15 |
16 | {php}
17 | App::after(function($request, $response)
18 | {
19 | // Capture last response
20 | $content = $response->getContent();
21 | File::put(storage_path().'/logs/last_response.txt', $content);
22 | });
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | You can modify the response.
28 |
29 | Since after filters receive the response object, you can make changes to the response within this filter. See [[Understanding the Request Lifecycle]] to understand exactly when application after filters are called.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/app/get-bootstrap.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Application Bootstrap File
3 | Topics: -
4 | Code: App::getBootstrapFile()
5 | Id: 198
6 | Position: 16
7 | ---
8 |
9 | {problem}
10 | You want to know the application bootstrap file.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::getBootstrapFile()`.
15 |
16 | This returns the full path to the bootstrap file.
17 |
18 | {php}
19 | echo App::getBootstrapFile();
20 | {/php}
21 |
22 | Will output something like.
23 |
24 | {text}
25 | /apps/example/vendor/laravel/framework/src/Illuminate/Foundation/start.php
26 | {/text}
27 | {/solution}
28 |
29 | {discussion}
30 | This is a low-level method.
31 |
32 | You can see where this file is loaded in [[Understanding the Request Lifecycle]]. In **The Booting Steps**, it's the block right after "Starts Application" labeled "laravel/start.php".
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/migrate-install.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating the Migration Repository
3 | Topics: artisan, migrations
4 | Code: -
5 | Id: 63
6 | Position: 11
7 | ---
8 |
9 | {problem}
10 | You want to create the migration repository.
11 |
12 | You know the migration repository holds a list of all your database migrations. Since you're ready to create some migrations you want to start by creating the repository.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan migrate:install` command.
17 |
18 | {php}
19 | $ php artisan migrate:install
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | You don't really need to do this.
25 |
26 | Laravel automatically creates the migration repository as needed. But if you want to check that it's created correctly, which is a good test that your database is configured right, then use the command.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/cache/xcache.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting up the XCache Cache Driver
3 | Topics: cache, configuration, XCache
4 | Code: -
5 | Id: 100
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to speed up Laravel's cache.
11 |
12 | You know by default Laravel uses the file cache driver. You want to use a speedier cache.
13 | {/problem}
14 |
15 | {solution}
16 | Use the XCache cache driver.
17 |
18 | Edit `app/config/cache.php` and change the driver to `'xcache'`.
19 |
20 | {php}
21 | 'driver' => 'xcache',
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | To use the cache driver make sure XCache is installed and set up first. See the [[Installing XCache]] recipe for details.
27 |
28 | {warn}
29 | You must change the `xcache.var_size` setting as described in the [[Installing XCache]] recipe for XCache to work with Laravel.
30 | {/warn}
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/clear-compiled.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Clearing the Compiled Class
3 | Topics: artisan
4 | Code: -
5 | Id: 59
6 | Position: 7
7 | ---
8 |
9 | {problem}
10 | You want to clear the compiled classes.
11 |
12 | You know that Larvel can optimize class loading and want to clear any optimizations for testing.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan clear-compiled' command.
17 |
18 | {php}
19 | $ php artisan clear-compiled
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | This deletes two files:
25 |
26 | 1. The `bootstrap/compiled.php` file. This file is created when you optimize classes.
27 | 2. The `app/storage/meta/services.json` file. This file is created as Laravel tries to optimize the loading of the service providers your application uses.
28 |
29 | See also [[Optimizing the Framework for Better Performance]].
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/clear-cache.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Clearing the Application Cache
3 | Topics: artisan, cache
4 | Code: Cache::flush()
5 | Id: 104
6 | Position: 23
7 | ---
8 |
9 | {problem}
10 | You want to clear your application cache.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan cache:clear' command.
15 |
16 | {php}
17 | $ php artisan cache:clear
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | This does two things.
23 |
24 | 1. `Cache::flush()` is called to empty the cache.
25 | 2. The `app/storage/meta/services.json` file is erased. This file is created
26 | as Laravel tries to optimize the loading of the service providers your
27 | application uses.
28 |
29 | _Please note that since this command deletes a file from the local file system, if you have multiple servers running your application you must execute it on each server._
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/file/is-writable.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining if a File or Directory is a Writable
3 | Topics: file system
4 | Code: File::isWritable(), is_writable()
5 | Id: 141
6 | Position: 17
7 | ---
8 |
9 | {problem}
10 | You want to check if your application has write access.
11 |
12 | Specifically, you want to check write access on a file or directory.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `File::isWritable()` method.
17 |
18 | {php}
19 | if (File::isWritable($filename))
20 | {
21 | echo "Yes. $filename is writable.";
22 | }
23 | if (File::isWritable($dirname))
24 | {
25 | echo "Yes. $dirname is writable.";
26 | }
27 | {/php}
28 |
29 | If the file or directory exists and is writable `true` is returned. Otherwise `false` is returned.
30 | {/solution}
31 |
32 | {discussion}
33 | This is a simple wrapper on the PHP `is_writable()` function.
34 | {/discussion}
35 |
--------------------------------------------------------------------------------
/docs/recipes/auth/changing-table.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Changing Your Authentication Table
3 | Topics: authentication, configuration
4 | Code: config/auth.php
5 | Id: 12
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You want your application's users in a different table than **users**.
11 |
12 | Your application is using **database** authentication and needs to use a different table than Laravel's default `users` table.
13 | {/problem}
14 |
15 | {solution}
16 | Edit `app/config/auth.php` to change the table.
17 |
18 | {php}
19 | 'table' => 'administrators',
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | Don't forget the required columns.
25 |
26 | Whatever the authentication table is set to, it must contain an `id` column, a `password` column, and some other column to authentication the user with. This other column is typically either `email` or `username`.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/auth/set-provider.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Authentication User Provider
3 | Topics: -
4 | Code: Auth::setProvider()
5 | Id: 236
6 | Position: 41
7 | ---
8 |
9 | {problem}
10 | You want to use a different user provider for authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::setProvider()` method.
15 |
16 | {php}
17 | Auth::setProvider($user_provider);
18 | {/php}
19 |
20 | The provider must implement `Illuminate\Auth\UserProviderInterface`.
21 | {/solution}
22 |
23 | {discussion}
24 | This is an advanced topic.
25 |
26 | Generally, the authentication driver you use will automatically set up the correct user provider.
27 |
28 | But this method is available for customization. Make sure you call this method before the authentication routines are accessed. Either a service provider or in `app/start/global.php` is a good location.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/html/email.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Obfuscating an Email Address
3 | Topics: html
4 | Code: HTML::email(), HTML::obfuscate()
5 | Id: 193
6 | Position: 13
7 | ---
8 |
9 | {problem}
10 | You want to obfuscate an e-mail address to prevent spam-bots from sniffing it.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `HTML::email()` method.
15 |
16 | This method takes one argument, the email address.
17 |
18 | {php}
19 | $email = HTML::email('me@local.com');
20 | {/php}
21 |
22 | Now `$email` will display correctly in browsers, but it will randomly contain characters that make it hard to read.
23 |
24 | {html}
25 | Email is me@local.com
26 | {/html}
27 | {/solution}
28 |
29 | {discussion}
30 | This method uses the `HTML::obfuscate()` method to obfuscate the email address.
31 |
32 | See [[Obfuscating a String]].
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/app/Recipes/Composers/LayoutsDefaultComposer.php:
--------------------------------------------------------------------------------
1 | groupBy('category_id')
13 | ->lists('num_recipes', 'category_id');
14 | $rows = Category::select(['id', 'name', 'slug'])
15 | ->orderBy('name')
16 | ->get();
17 | $categories = [];
18 | foreach ($rows as $row)
19 | {
20 | $categories[$row->id] = [
21 | 'name' => $row->name,
22 | 'slug' => $row->slug,
23 | 'num_recipes' => $counts[$row->id],
24 | ];
25 | }
26 |
27 | $view->with(compact('categories'));
28 | }
29 | }
--------------------------------------------------------------------------------
/docs/recipes/auth/create-db-driver.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating an Instance of the Auth Database Driver
3 | Topics: -
4 | Code: Auth::createDatabaseDriver()
5 | Id: 213
6 | Position: 18
7 | ---
8 |
9 | {problem}
10 | You want to create an instance of the database driver.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::createDatabaseDriver()` method.
15 |
16 | {php}
17 | $driver = Auth::createDatabaseDriver();
18 | {/php}
19 |
20 | Once you have the `$driver` above you can call the standard `check()`, `guest()`, `user()`, etc. methods directly on the driver.
21 | {/solution}
22 |
23 | {discussion}
24 | Generally, it's better to use the `Auth` facade.
25 |
26 | This sets up the appropriate driver based on your configuration driver. But if your application is complex, you can instantiate the driver separately to have multiple authentication drivers active at one time.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/auth/create-el-driver.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating an Instance of the Auth Eloquent Driver
3 | Topics: -
4 | Code: Auth::createEloquentDriver()
5 | Id: 214
6 | Position: 19
7 | ---
8 |
9 | {problem}
10 | You want to create an instance of the Eloquent driver.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::createEloquentDriver()` method.
15 |
16 | {php}
17 | $driver = Auth::createEloquentDriver();
18 | {/php}
19 |
20 | Once you have the `$driver` above you can call the standard `check()`, `guest()`, `user()`, etc. methods directly on the driver.
21 | {/solution}
22 |
23 | {discussion}
24 | Generally, it's better to use the `Auth` facade.
25 |
26 | This sets up the appropriate driver based on your configuration driver. But if your application is complex, you can instantiate the driver separately to have multiple authentication drivers active at one time.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/file/get.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Contents of a File
3 | Topics: file system
4 | Code: File::get()
5 | Id: 126
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You want to load the contents of a file.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::get()` method.
15 |
16 | {php}
17 | $contents = File::get($filename);
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | If the file isn't found an exception is returned.
23 |
24 | Specifically, a `Illuminate\Filesystem\FileNotFoundException` exception is returned.
25 |
26 | If you don't explicitly test if the file exists yourself, it's good practice to wrap the call in a try/catch block.
27 |
28 | {php}
29 | try
30 | {
31 | $contents = File::get($filename);
32 | }
33 | catch (Illuminate\Filesystem\FileNotFoundException $exception)
34 | {
35 | die("The file doesn't exist");
36 | }
37 | {/php}
38 | {/discussion}
39 |
--------------------------------------------------------------------------------
/docs/recipes/form/button.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating a Button Element
3 | Topics: forms
4 | Code: Form::button()
5 | Id: 172
6 | Position: 22
7 | ---
8 |
9 | {problem}
10 | You want to create a button element in your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Form::button()` method.
15 |
16 | Specify the value as the first argument.
17 |
18 | {html}
19 | {{ Form::button('Hit Me') }}
20 | {/html}
21 |
22 | The HTML produced is very simple.
23 |
24 | {html}
25 |
26 | {/html}
27 |
28 | Use the second argument to add additional attributes.
29 |
30 | {html}
31 | {{ Form::button('Hit Me', array('class' => 'btn')) }}
32 | {/html}
33 |
34 | And now resulting the button has a class.
35 |
36 | {html}
37 |
38 | {/html}
39 | {/solution}
40 |
41 | {discussion}
42 | Nothing to discuss.
43 | {/discussion}
44 |
--------------------------------------------------------------------------------
/docs/recipes/html/decode.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Decoding HTML Entities to a String
3 | Topics: html
4 | Code: HTML::decode(), HTML::entities()
5 | Id: 182
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You have a string encoded by `HTML::entities()` and want to decode it back to the original string.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `HTML::decode()` method.
15 |
16 | This is the opposite of `HTML::entities()`. For example.
17 |
18 | {php}
19 | $string HTML::decode('<h1>Hello</h1>');
20 | {/php}
21 |
22 | In this example `$string` will equal "<h1>Hello</h1>";
23 | {/solution}
24 |
25 | {discussion}
26 | This method wraps the PHP `html_entity_decode()` function.
27 |
28 | Specifically it calls `html_entity_decode($value, ENT_QUOTES, 'UTF-8')`. These options compliment the ones passed to `htmlentities()` by the `HTML::entities()` method.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/app/set-locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Set the Current Application Locale
3 | Topics: -
4 | Code: App::setLocale()
5 | Id: 208
6 | Position: 26
7 | ---
8 |
9 | {problem}
10 | You want to change your application's locale.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::setLocale()` method.
15 |
16 | {php}
17 | // Change to Spanish
18 | App::setLocale('es');
19 | {/php}
20 |
21 | Now the locale is `es` for the remainder of the request.
22 |
23 | Remember: the next request the locale will be back to whatever the configuration specifies.
24 | {/solution}
25 |
26 | {discussion}
27 | This does more than simply changing `app.locale` in the configuration.
28 |
29 | It does three things:
30 |
31 | 1. Changes `app.locale` in the currently loaded configuration values.
32 | 2. Sets the locale of the translator.
33 | 3. Fires a `locale.changed` event which your application can listen for.
34 | {/discussion}
35 |
--------------------------------------------------------------------------------
/docs/recipes/auth/attempting.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Listening for Authentication Attempts
3 | Topics: -
4 | Code: Auth::attempting()
5 | Id: 220
6 | Position: 25
7 | ---
8 |
9 | {problem}
10 | You want to execute code whenever an authentication attempt is made.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::attempting()` method.
15 |
16 | {php}
17 | Auth::attempting(function($credentials, $remember, $login)
18 | {
19 | // Log the attempt or some other such activity
20 | });
21 | {/php}
22 |
23 | Note this fires when the attempt is made, whether or not the attempt would be successful.
24 | {/solution}
25 |
26 | {discussion}
27 | Where should the code for listening for authentication attempts go?
28 |
29 | If you have a helpers file for event listening, use that (see [[Creating a Helpers File]]).
30 |
31 | Otherwise you can put the code in a service provider or `app/start/global.php`.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/serve.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Running PHP's Built-in Web Server
3 | Topics: artisan
4 | Code: -
5 | Id: 282
6 | Position: 29
7 | ---
8 |
9 | {problem}
10 | You want to quickly test your Laravel application.
11 |
12 | But you don't want to set up a web server.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan serve` command.
17 |
18 | {bash}
19 | $ php artisan serve
20 | Laravel development server started on http://localhost:8000
21 | {/bash}
22 |
23 | Now you can point your browser to `http://localhost:8000` and see your application.
24 |
25 | Press `Ctrl-C` to stop the server.
26 | {/solution}
27 |
28 | {discussion}
29 | Use the `--port` option to specify a different port.
30 |
31 | If you want a port other than default, just specify it.
32 |
33 | {bash}
34 | $ php artisan serve --port=8080
35 | Laravel development server started on http://localhost:8080
36 | {/bash}
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/file/clean-directory.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Emptying a Directory of All Files and Folders
3 | Topics: file system
4 | Code: File::cleanDirectory(), File::deleteDirectory()
5 | Id: 150
6 | Position: 26
7 | ---
8 |
9 | {problem}
10 | You want to empty a directory completely of it's contents.
11 |
12 | But you want to retain the directory itself.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `File::cleanDirectory()` method.
17 |
18 | {php}
19 | $success = File::cleanDirectory($directory);
20 | {/php}
21 |
22 | The method will return `false` if the directory doesn't exist. Otherwise it returns `true` when complete.
23 | {/solution}
24 |
25 | {discussion}
26 | This is a wrapper on `File::deleteDirectory()`.
27 |
28 | It operates just as `File::deleteDirectory()` does but it always retains the directory instead of deleting it.
29 |
30 | See [[Recursively Deleting a Directory]] for more details.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_16_112029_create_sections_table.php:
--------------------------------------------------------------------------------
1 | increments('id');
18 | $table->string('name')->unique();
19 | $table->string('description');
20 | $table->integer('position');
21 | $table->timestamps();
22 | $table->engine = 'MyISAM';
23 | });
24 | }
25 |
26 | /**
27 | * Reverse the migrations.
28 | *
29 | * @return void
30 | */
31 | public function down()
32 | {
33 | Schema::drop('sections');
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/auth-remind.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating a Migration for Password Reminders
3 | Topics: artisan, authentication, password reminders
4 | Code: -
5 | Id: 71
6 | Position: 19
7 | ---
8 |
9 | {problem}
10 | You want to create a table to store password reminders.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan auth:reminders` command.
15 |
16 | {php}
17 | $ php artisan auth:reminders
18 | {/php}
19 |
20 | This creates a new migration which will create a `password_reminders` table.
21 |
22 | After creating the migration, don't forget to run migrations to update your database with the new table.
23 |
24 | {php}
25 | $ php artisan migrate
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | Laravel provides a skeleton for implementing password reminders.
31 |
32 | You'll still need to fill in the details for your application. Creating the reminders table is only one of the steps.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/auth/get-user.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Currently Cached Authenticated User
3 | Topics: -
4 | Code: Auth::getUser(), Auth::user()
5 | Id: 229
6 | Position: 34
7 | ---
8 |
9 | {problem}
10 | You want to get the currently cached authenticated user.
11 |
12 | But if there is no user cached, you do not want to attempt authentication.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Auth::getUser()` method.
17 |
18 | {php}
19 | $user = Auth::getUser();
20 | if ( ! $user)
21 | {
22 | echo "No user is currently authenticated.";
23 | }
24 | {/php}
25 | {/solution}
26 |
27 | {discussion}
28 | The `Auth::getUser()` and `Auth::user()` methods are slightly different.
29 |
30 | That `Auth::user()` will attempt to authenticate a user from the session or the "remember me" cookie if there is not a user cached.
31 |
32 | `Auth::getUser()` only returns a user if they've already been authenticated successfully.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/file/delete.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Deleting a File
3 | Topics: file system
4 | Code: File::delete(), File::exists(), unlink()
5 | Id: 133
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You want to delete a file on the filesystem.
11 |
12 | You know you could use PHP's `unlink()` method, but want to do it the Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `File::delete()` method.
17 |
18 | {php}
19 | // Delete a single file
20 | File::delete($filename);
21 |
22 | // Delete multiple files
23 | File::delete($file1, $file2, $file3);
24 |
25 | // Delete an array of files
26 | $files = array($file1, $file2);
27 | File::delete($files);
28 | {/php}
29 | {/solution}
30 |
31 | {discussion}
32 | Errors are quietly ignored.
33 |
34 | If there's a problem deleting the file, any error is silently ignored. If it's important that a file was deleted, check it's existence after deleting it with `File::exists()`.
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/html/link-asset.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Generating a HTML Link to an Asset
3 | Topics: html
4 | Code: HTML::link(), HTML::linkAsset()
5 | Id: 188
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to create a link to one of your application's assets.
11 |
12 | You could just use the anchor tag directly, but you're using a Blade template and want to do it the Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `HTML::linkAsset()` method.
17 |
18 | This method takes the exact arguments as `HTML::link()` (see [[Generating a HTML Link]]).
19 | {/solution}
20 |
21 | {discussion}
22 | There's a small difference between links to web pages and links to assets.
23 |
24 | Depending on your application's configuration, your web pages may have `index.php` at the start of the path. Assets never do.
25 |
26 | When determining the URL for the asset, `HTML::linkAsset()` will strip away any `index.php` if it's there.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/cache/apc.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting up the APC Cache Driver
3 | Topics: apc, cache, configuration
4 | Code: -
5 | Id: 92
6 | Position: 3
7 | ---
8 |
9 | {problem}
10 | You want to speed up Laravel's cache.
11 |
12 | You know by default Laravel uses the file cache driver. You want to use a speedier cache.
13 | {/problem}
14 |
15 | {solution}
16 | Use the APC cache driver.
17 |
18 | Edit `app/config/cache.php` and change the driver to `'apc'`.
19 |
20 | {php}
21 | 'driver' => 'apc',
22 | {/php}
23 |
24 | That's it.
25 | {/solution}
26 |
27 | {discussion}
28 | Make sure APC is installed first.
29 |
30 | See the [[Installing APC]] recipe for details.
31 |
32 | You can use the `apc.php` script that comes with APC to monitor your cache. Usually, this file is placed in the `/usr/share/doc/php5-apcu` directory. Copy the file to your public folder, then bring up `http://yourapp/apc.php` to view the cache statistics.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/config/set.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting a Specific Configuration Value
3 | Topics: configuration
4 | Code: Config::get(), Config::set()
5 | Id: 39
6 | Position: 4
7 | ---
8 |
9 | {problem}
10 | You want to set a configuration option.
11 |
12 | But you don't want this value to be stored as part of your permanent configuration.
13 | {/problem}
14 |
15 | {solution}
16 | Use `Config::set()`
17 |
18 | Let's say you want to set the session driver to use the array driver.
19 |
20 | {php}
21 | Config::set('session.driver', 'array');
22 | {/php}
23 |
24 | Now, for the remainer of the request, any time `Config::get('session.driver')` is called, the value `array` will be returned.
25 | {/solution}
26 |
27 | {discussion}
28 | The only affects the current request.
29 |
30 | If you want to change a configuration value permanently, so all future requests will retrieve the same value, you must edit the appropriate configuration file.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/install/apc.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing APC
3 | Topics: apc, cache, installation
4 | Code: -
5 | Id: 91
6 | Position: 10
7 | ---
8 |
9 | {problem}
10 | You want to speed up your application using a cache.
11 | {/problem}
12 |
13 | {solution}
14 | Install APC (Alternative PHP Cache)
15 |
16 | It's easy to install with just two lines from your console.
17 |
18 | {bash}
19 | $ sudo apt-get install -y php-apc
20 | $ sudo service apache2 restart
21 | {/bash}
22 |
23 | The first line installs the package, the second restarts apache.
24 | {/solution}
25 |
26 | {discussion}
27 | APC can help your application in two ways.
28 |
29 | First of all, is APC's opcode cache. This speeds up the execution of your program by caching the results of PHP's byte-code compiler.
30 |
31 | Secondly, you can use APC as a cache within your application. See [[Setting up the APC Cache Driver]] for instructions on using APC as the cache driver.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/migrate-reset.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Rolling Back All Database Migrations
3 | Topics: artisan, migrations
4 | Code: -
5 | Id: 67
6 | Position: 15
7 | ---
8 |
9 | {problem}
10 | You want to undo all database migrations.
11 |
12 | You want a clean database, with no tables in it.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan migrate:reset`.
17 |
18 | {php}
19 | $ php artisan migrate:reset
20 | {/php}
21 |
22 | You can use the `--pretend` option to see what would happen without affecting any data.
23 |
24 | {php}
25 | $ php artisan migrate:reset --pretend
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | This is like calling `migrate:rollback` over and over again.
31 |
32 | In fact, you could repeatedly call `php artisan migrate:rollback` and there's nothing left to rollback.
33 |
34 | The only table left in your database when this command completes is the `migrations` table and it will be empty.
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/file/all-files.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting All the Files Recursively From a Directory
3 | Topics: file system
4 | Code: File::allFiles(), SplFileInfo
5 | Id: 145
6 | Position: 21
7 | ---
8 |
9 | {problem}
10 | You want to fetch all the files recursively from your file system.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `File::allFiles()` method.
15 |
16 | {php}
17 | $files = File::allFiles($directory);
18 | foreach ($files as $file)
19 | {
20 | echo (string)$file, "\n";
21 | }
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | The method returns an array of `SplFileInfo` objects.
27 |
28 | Specifically, it returns a `Symfony\Component\Finder\SplFileInfo` object which is derived from the basic PHP `SplFileInfo` class to add relative paths. Each object returned will represent a single file _(no directories are returned)_.
29 |
30 | If the directory doesn't exist, an `InvalidArgumentException` will be thrown.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_063648_create_category_table.php:
--------------------------------------------------------------------------------
1 | increments('id');
16 | $table->string('slug')->unique();
17 | $table->integer('section_id')->unsigned()->index();
18 | $table->string('name');
19 | $table->string('description');
20 | $table->integer('position');
21 | $table->timestamps();
22 | $table->engine = 'MyISAM';
23 | });
24 | }
25 |
26 | /**
27 | * Reverse the migrations.
28 | */
29 | public function down()
30 | {
31 | Schema::drop('categories');
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/docs/recipes/app/set-request-console.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Request for the Console Environment
3 | Topics: -
4 | Code: App::setRequestForConsoleEnvironment()
5 | Id: 211
6 | Position: 28
7 | ---
8 |
9 | {problem}
10 | You have a console application and want to set the request.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::setRequestForConsoleEnvironment()`.
15 |
16 | Usually, you don't need the whole HTTP layer Laravel provides when developing console utilities. But when you do, you can use this command.
17 |
18 | {php}
19 | App::setRequestForConsoleEnvironment();
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | This is most handy with unit testing.
25 |
26 | When unit testing sometimes it's nice to "fake" a request environment.
27 |
28 | If you're deriving your unit tests from the file Laravel provides for you (`app/tests/TestCase.php`) the `App::setRequestForConsoleEnvironment()` is called automatically for you.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/migrate.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Running Database Migrations
3 | Topics: artisan, migrations
4 | Code: -
5 | Id: 65
6 | Position: 13
7 | ---
8 |
9 | {problem}
10 | You want to run your database migrations.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan migrate` command.
15 |
16 | {php}
17 | $ php artisan migrate
18 | {/php}
19 |
20 | You'll see a list of what migrations ran.
21 |
22 | If you just want to see what _would_ happen if you were to migrate, use the `--pretend` option.
23 |
24 | {php}
25 | $ php artisan migrate --pretend
26 | {/php}
27 |
28 | This will show you everything that would happen to your data, but doesn't actually make any changes.
29 | {/solution}
30 |
31 | {discussion}
32 | You can seed your database too.
33 |
34 | Use the `--seed` option and your database will be seeded after the migrations occur. That is, if you have any seeds set up.
35 |
36 | See [[Seeding Your Database]] recipe.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/form/reset.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating a Reset Input Field
3 | Topics: forms
4 | Code: Form::reset()
5 | Id: 169
6 | Position: 19
7 | ---
8 |
9 | {problem}
10 | You want to create a reset button in your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Form::reset()` method.
15 |
16 | You only need to supply the value as the first argument.
17 |
18 | {html}
19 | {{ Form::reset('Clear form') }}
20 | {/html}
21 |
22 | The HTML produced is.
23 |
24 | {html}
25 |
26 | {/html}
27 |
28 | If you want to add additional attributes, add a second argument with an array of attributes.
29 |
30 | {html}
31 | {{ Form::reset('Clear form', ['class' => 'form-button']) }}
32 | {/html}
33 |
34 | Now the output has a class attribute.
35 |
36 | {html}
37 |
38 | {/html}
39 | {/solution}
40 |
41 | {discussion}
42 | Nothing to discuss.
43 | {/discussion}
44 |
--------------------------------------------------------------------------------
/docs/recipes/lang/get-select.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Translation Message Selector
3 | Topics: -
4 | Code: Lang::getSelector()
5 | Id: 259
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to use the message selector logic the translation service is using.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Lang::getSelector()` to retrieve the instance.
15 |
16 | {php}
17 | $selector = Lang::getSelector();
18 |
19 | $result = $selector->choose('message.key', 3, 'en');
20 | echo $result;
21 | {/php}
22 |
23 | Given a message with different plural translations separated by a pipe (|), the `$selector->choose()` method returns the correct portion of the message based on the given number, locale, and the pluralizaion rules in the message itself.
24 |
25 | The class of the selector is `Symfony\Component\Translation\MessageSelector`.
26 | {/solution}
27 |
28 | {discussion}
29 | Only in the rarest of cases will using this method be needed.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ChuckHeintzelman/laravel-recipes",
3 | "description": "A bunch of little Laravel 'how-tos'.",
4 | "license": "Creative Commons Attribution-ShareAlike 4.0 International License",
5 | "require": {
6 | "laravel/framework": "4.2.*",
7 | "dflydev/markdown" : "1.0.*@dev"
8 | },
9 | "autoload": {
10 | "psr-0": {
11 | "Recipes": "app/"
12 | },
13 | "files": [
14 | "app/helpers.php"
15 | ],
16 | "classmap": [
17 | "app/database/migrations",
18 | "app/database/seeds",
19 | "app/tests/TestCase.php"
20 | ]
21 | },
22 | "scripts": {
23 | "post-install-cmd": [
24 | "php artisan clear-compiled",
25 | "php artisan optimize"
26 | ],
27 | "post-update-cmd": [
28 | "php artisan clear-compiled",
29 | "php artisan optimize"
30 | ],
31 | "post-create-project-cmd": [
32 | "php artisan key:generate"
33 | ]
34 | },
35 | "config": {
36 | "preferred-install": "dist"
37 | },
38 | "minimum-stability": "stable"
39 | }
40 |
--------------------------------------------------------------------------------
/docs/recipes/app/shutdown.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Calling the Registered Shutdown Callbacks
3 | Topics: callbacks
4 | Code: App::shutdown()
5 | Id: 56
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You want to call any registered shutdown functions directly.
11 |
12 | You're exiting your Laravel application in a non-standard way, and want to call any shutdown callbacks that have been registered with your application.
13 | {/problem}
14 |
15 | {solution}
16 | Use `App::shutdown()`
17 |
18 | You can call `App::shutdown()` without any arguments to automatically call what's registered.
19 |
20 | {php}
21 | // Call registered callbacks
22 | App::shutdown();
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | This is non-standard.
28 |
29 | Calling this method directly is not part of the standard request lifecycle.
30 |
31 | Also, note that calling `App::shutdown()` does not actually exit your application, it only calls the registered shutdown callbacks.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/asset-publish.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Publishing a Package's Assets to Your Public Directory
3 | Topics: artisan
4 | Code: -
5 | Id: 279
6 | Position: 26
7 | ---
8 |
9 | {problem}
10 | You want to copy a third party package's assets to your application.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan asset:publish` command.
15 |
16 | {bash}
17 | $ php artisan asset:publish cool-package
18 | {/bash}
19 |
20 | This copies the assets from the third party package into your application's `public/packages/cool-package` directory, making them available in the web space.
21 | {/solution}
22 |
23 | {discussion}
24 | For non-Laravel packages you may need to specify the path.
25 |
26 | {bash}
27 | $ php artisan asset:publish cool-package --path=/package/dir
28 | {/bash}
29 |
30 | For workbench packages, you can simply specify the workbench name.
31 |
32 | {bash}
33 | $ php artisan asset:publish --bench=cool-package
34 | {/bash}
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/auth/set-dispatch.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Event Dispatcher for Authentication
3 | Topics: -
4 | Code: Auth::setDispatcher()
5 | Id: 232
6 | Position: 37
7 | ---
8 |
9 | {problem}
10 | You want to use a different event dispatcher for authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::setDispatcher()` method.
15 |
16 | {php}
17 | Auth::setDispatcher($events);
18 | {/php}
19 |
20 | The dispatcher must be derived from `Illuminate\Events\Dispatcher`.
21 | {/solution}
22 |
23 | {discussion}
24 | This is an advanced topic.
25 |
26 | Most of the time the standard event dispatcher created by Laravel works just fine. The dispatcher the `Auth` facade uses is the same one the `Event` facade uses.
27 |
28 | But, if you need to use a dispatcher this method is available. Make sure you call this method before the authentication routines are accessed. Either a service provider or in `app/start/global.php` is a good location.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/lang/has.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining if a Translation Exists
3 | Topics: localization
4 | Code: Lang::has()
5 | Id: 251
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You want to determine if a translation exists for a particular key.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Lang::has()` method.
15 |
16 | To check if a translation exists for the current locale pass a single argument to the method. The argument is the _key_ you're checking.
17 |
18 | {php}
19 | if (Lang::has('message.welcome'))
20 | {
21 | echo "The welcome message translation exists for the current locale";
22 | }
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | You can specify the locale to check with the second argument.
28 |
29 | {php}
30 | if (Lang::has('message.welcome', 'es'))
31 | {
32 | echo "The welcome message translation exists for Spanish";
33 | }
34 | {/php}
35 |
36 | If the second argument isn't used then the current locale is used.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/auth/changing-driver.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Changing Your Authentication Driver
3 | Topics: authentication, configuration
4 | Code: config/auth.php
5 | Id: 10
6 | Position: 7
7 | ---
8 |
9 | {problem}
10 | You don't want to use the Eloquent authentication driver.
11 |
12 | You want to use Laravel's built in `Auth` package, but don't want to use the default driver.
13 | {/problem}
14 |
15 | {solution}
16 | Switch to the **database** authentication driver.
17 |
18 | Edit `app/config/auth.php` and change the driver.
19 |
20 | {php}
21 | 'driver' => 'database',
22 | {/php}
23 |
24 | If you're using a different table than **users** then see [[Changing Your Authentication Table]].
25 | {/solution}
26 |
27 | {discussion}
28 | Out of the box you have two choices.
29 |
30 | Laravel only provides an **eloquent** option and a **database** option out of the box. You can create your own driver though. See [[Using Your Own Authentication Driver]] for details.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/auth/set-cookie.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Cookie Jar Used for Authentication
3 | Topics: -
4 | Code: Auth::setCookieJar()
5 | Id: 226
6 | Position: 31
7 | ---
8 |
9 | {problem}
10 | You want to use a different cookie creator for authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::setCookieJar()` method.
15 |
16 | {php}
17 | Auth::setCookieJar($cookie_jar);
18 | {/php}
19 |
20 | The cookie jar must be derived from `Illuminate\Cookie\CookieJar`.
21 | {/solution}
22 |
23 | {discussion}
24 | This is an advanced topic.
25 |
26 | Most of the time the standard cookie handler created by Laravel works just fine. The cookie jar the `Auth` facade uses is the same one the `Cookie` facade uses.
27 |
28 | But, if you need to use a different cookie provider this method is available. Make sure you call this method before the authentication routines are accessed. Either a service provider or in `app/start/global.php` is a good location.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/crypt/whereused.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Understanding Where Laravel Uses Encryption
3 | Topics: cookies, encryption
4 | Code: -
5 | Id: 110
6 | Position: 6
7 | ---
8 |
9 | {problem}
10 | You want to understand where and when Laravel uses encryption.
11 | {/problem}
12 |
13 | {solution}
14 | There's only three places Laravel uses the Crypt package.
15 |
16 | 1. Caches. Specifically if you're using the Database Cache driver then values
17 | placed in the cache are encrypted prior to saving and decrypted when loaded.
18 | 2. Cookies. Cookie values are always sent to the user encrypted. When the
19 | request loads, all cookie values are decrypted.
20 | 3. Queues. Specifically if you're using the iron queue driver then values are
21 | stored in the queue encrypted and decrypted once retrieved.
22 | {/solution}
23 |
24 | {discussion}
25 | See also [[Understanding the Request Lifecycle]].
26 |
27 | Keep in mind that cookies are handled as middleware.
28 | {/discussion}
29 |
--------------------------------------------------------------------------------
/app/Recipes/Unused/Vars.php:
--------------------------------------------------------------------------------
1 | key = $key;
18 | $row->value = $encoded;
19 | }
20 | else
21 | {
22 | $row->value = $encoded;
23 | }
24 | $row->save();
25 | }
26 |
27 | /**
28 | * Return a PHP value from the vars table
29 | */
30 | public static function get($key, $default = null)
31 | {
32 | $row = static::find($key);
33 | if (is_null($row))
34 | {
35 | return $default;
36 | }
37 | return json_decode($row->value, true);
38 | }
39 | }
--------------------------------------------------------------------------------
/docs/recipes/auth/set-request.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Request Instance for Authentication
3 | Topics: -
4 | Code: Auth::setRequest()
5 | Id: 234
6 | Position: 39
7 | ---
8 |
9 | {problem}
10 | You want to use a different request instance for authentication.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::setRequest()` method.
15 |
16 | {php}
17 | Auth::setRequest($request);
18 | {/php}
19 |
20 | The dispatcher must be derived from `\Symfony\Component\HttpFoundation\Request`.
21 | {/solution}
22 |
23 | {discussion}
24 | This is most often used in testing.
25 |
26 | Most of the time the standard request created by Laravel works just fine. The dispatcher the `Auth` facade uses is the same one the `Request` facade uses.
27 |
28 | But, if you need to set a different request this method is available. Make sure you call this method before the authentication routines are accessed. Either a service provider or in `app/start/global.php` is a good location.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/crypt/decrypt.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Decrypting a Value
3 | Topics: encryption
4 | Code: Config::get(), Crypt::encrypt(), Crypt::setKey()
5 | Id: 106
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You want to decrypt a string.
11 |
12 | You've encrypted a string and are ready to decrypt and use it.
13 | {/problem}
14 |
15 | {solution}
16 | Use `Crypt::decrypt()`.
17 |
18 | {php}
19 | $value = Crypt::decrypt($encrypted);
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | You must decrypt the value with the same key used to encrypt it.
25 |
26 | Laravel's encryption routines use `Config::get('app.key')` for encryption. This happens internally. Since this value is different for every Laravel application then the application that encrypts a value must also decrypt the value.
27 |
28 | Or ...
29 |
30 | The application must call `Crypt::setKey()` prior to decrypting to match the key to the value used for encrypting. See [[Setting the Encryption Key]].
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/html/attributes.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Bulding an HTML Attribute String From an Array
3 | Topics: html
4 | Code: Form::macro(), HTML::attributes(), HTML::macro()
5 | Id: 196
6 | Position: 16
7 | ---
8 |
9 | {problem}
10 | You have an associative array of attributes for an HTML element and want to convert it to a string.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `HTML::attributes()` method.
15 |
16 | {php}
17 | echo HTML::attributes(array('id' => '123', 'class' => 'myclass'));
18 | {/php}
19 |
20 | The above will build a string where the keys of the array are the attribute names and the values of the array are the attribute values. The output will be.
21 |
22 | {text}
23 | id="123" class="myclass"
24 | {/text}
25 | {/solution}
26 |
27 | {discussion}
28 | This is useful in HTML or Form macros.
29 |
30 | It's useful whenever you need to build tag attributes in HTML or even XML.
31 |
32 | See [[Creating Form Macros]]
33 | and [[Creating HTML Macros]].
34 | {/discussion}
35 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_103853_create_code_recipe_pivot.php:
--------------------------------------------------------------------------------
1 | increments('id');
18 | $table->integer('code_id')->unsigned();
19 | $table->integer('recipe_id')->unsigned();
20 | $table->unique(['code_id', 'recipe_id']);
21 | $table->unique(['recipe_id', 'code_id']);
22 | $table->engine = 'MyISAM';
23 | });
24 | }
25 |
26 | /**
27 | * Reverse the migrations.
28 | *
29 | * @return void
30 | */
31 | public function down()
32 | {
33 | Schema::drop('code_recipe');
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/docs/recipes/cache/add.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Storing an Item in the Cache if it Doesn't Exist
3 | Topics: cache
4 | Code: Cache::add()
5 | Id: 267
6 | Position: 16
7 | ---
8 |
9 | {problem}
10 | You want to store an item in the cache.
11 |
12 | But you only want to store it if the item doesn't already exist.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Cache::add()` method.
17 |
18 | {php}
19 | $result = Cache::add($key, $value, $minutes);
20 | if ($result)
21 | {
22 | echo "$key stored for $minutes";
23 | }
24 | else
25 | {
26 | echo "$key wasn't stored, already in cache";
27 | }
28 | {/php}
29 |
30 | If the item was stored, `true` is returned. Otherwise `false` is returned.
31 | {/solution}
32 |
33 | {discussion}
34 | A couple of notes about this method.
35 |
36 | 1. If `false` is returned, you should call `Cache::get()` to retrieve the item.
37 | 2. If the key exists in the cache and is `null`, the new value will **always** be stored and `true` returned.
38 | {/discussion}
39 |
--------------------------------------------------------------------------------
/docs/recipes/crypt/setcipher.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Encryption Cypher
3 | Topics: encryption
4 | Code: Crypt::setCipher(), mcrypt_list_algorithms()
5 | Id: 108
6 | Position: 4
7 | ---
8 |
9 | {problem}
10 | You want to change the cipher the encryption component uses.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Crypt::setCipher()`.
15 |
16 | {php}
17 | Crypt::setCipher($new_cipher);
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | The default cipher method is `rijndael-256`.
23 |
24 | You can get a list of available ciphers with a call to `mcrypt_list_algorithms()`.
25 |
26 | Here's a partial list:
27 |
28 | * cast-128
29 | * gost
30 | * rijndael-128
31 | * twofish
32 | * arcfour
33 | * cast-256
34 | * loki97
35 | * rijndael-192
36 | * saferplus
37 | * wake
38 | * blowfish-compat
39 | * des
40 | * rijndael-256
41 |
42 | This cipher will be used for subsequent `Crypt::encrypt()` and `Crypt::decrypt()` calls.
43 |
44 | But only in the current request.
45 | {/discussion}
46 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_103829_create_recipe_topic_pivot.php:
--------------------------------------------------------------------------------
1 | increments('id');
18 | $table->integer('recipe_id')->unsigned();
19 | $table->integer('topic_id')->unsigned();
20 | $table->unique(['recipe_id', 'topic_id']);
21 | $table->unique(['topic_id', 'recipe_id']);
22 | $table->engine = 'MyISAM';
23 | });
24 | }
25 |
26 | /**
27 | * Reverse the migrations.
28 | *
29 | * @return void
30 | */
31 | public function down()
32 | {
33 | Schema::drop('recipe_topic');
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/docs/recipes/auth/get-provider.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Authentication User Provider
3 | Topics: -
4 | Code: Auth::getProvider()
5 | Id: 235
6 | Position: 40
7 | ---
8 |
9 | {problem}
10 | You want to access the user provider the authentication uses.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::getProvider()` method.
15 |
16 | {php}
17 | $provider = Auth::getProvider();
18 | {/php}
19 | {/solution}
20 |
21 | {discussion}
22 | The provider will be based on how your authentication is configured.
23 |
24 | It'll be the provider for the driver you set up in the your `app/config/auth.php`. See [[Changing Your Authentication Driver]].
25 |
26 | If you're using the `database` configuration driver, the provider will be `Illuminate\Auth\DatabaseUserProvider`. If you're using the `eloquent` configuration driver the provider will be `Illuminate\Auth\EloquentUserProvider`.
27 |
28 | If you set up a custom authentication driver, the user provider will be whatever you set up.
29 | {/discussion}
30 |
--------------------------------------------------------------------------------
/docs/recipes/blade/lang.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Outputting a Translation in a Blade Template
3 | Topics: -
4 | Code: @lang, Lang::get()
5 | Id: 250
6 | Position: 20
7 | ---
8 |
9 | {problem}
10 | You want to use translations in your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the Blade `@lang` command.
15 |
16 | For example:
17 |
18 | {html}
19 | @lang('messages.welcome')
20 | {/html}
21 |
22 | Assuming there's a `welcome` key in the `messages.php` for the current locale, the above would output the translated message.
23 |
24 | If there is no translated message then `messages.welcome` would be output.
25 |
26 | If your message has a placeholder, you can pass a second parameter with an array of placeholders.
27 |
28 | {html}
29 | @lang('messages.welcome', ['name' => $name])
30 | {/html}
31 | {/solution}
32 |
33 | {discussion}
34 | This Blade command uses the `Lang::get()` method.
35 |
36 | See [[Getting the Translation for a Key]] for details about this method.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/auth/password-table.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Changing Your Password Reminder Table
3 | Topics: authentication, configuration
4 | Code: -
5 | Id: 78
6 | Position: 12
7 | ---
8 |
9 | {problem}
10 | You want to use a different password reminder table.
11 |
12 | You know Laravel is set up to use the `password_reminders` table by default, but you want a different table for your application.
13 | {/problem}
14 |
15 | {solution}
16 | Edit your `app/config/auth.php` file.
17 |
18 | {php}
19 | 'reminder' => array(
20 | 'table' => 'password_reminders',
21 | ),
22 | {/php}
23 |
24 | Change the table value to the table name you wish to use.
25 | {/solution}
26 |
27 | {discussion}
28 | Don't forget to update your migrations.
29 |
30 | If you're using artisan to create the table (see [[Creating a Migration for Password Reminders]]), make sure you update your migration.
31 |
32 | Or you could create a migration for your table from scratch. See [[Creating a New Migration]].
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/app/config/workbench.php:
--------------------------------------------------------------------------------
1 | '',
17 |
18 | /*
19 | |--------------------------------------------------------------------------
20 | | Workbench Author E-Mail Address
21 | |--------------------------------------------------------------------------
22 | |
23 | | Like the option above, your e-mail address is used when generating new
24 | | workbench packages. The e-mail is placed in your composer.json file
25 | | automatically after the package is created by the workbench tool.
26 | |
27 | */
28 |
29 | 'email' => '',
30 |
31 | );
32 |
--------------------------------------------------------------------------------
/docs/recipes/crypt/encrypt.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Encrypting a Value
3 | Topics: encryption
4 | Code: Config::get(), Crypt::encrypt(), Crypt::setKey()
5 | Id: 105
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to encrypt a string.
11 |
12 | You have private data and want to encrypt it for later decryption.
13 | {/problem}
14 |
15 | {solution}
16 | Use `Crypt::encrypt()`.
17 |
18 | {php}
19 | $encrypted = Crypt::encrypt($value);
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | You must later decrypt the value with the same key used to encrypt it.
25 |
26 | Laravel's encryption routines use `Config::get('app.key')` for encryption. This happens internally. Since this value is different for every Laravel application then a value encrypted by the application should only be decrypt with the same application.
27 |
28 | Or ...
29 |
30 | The application can call `Crypt::setKey()` prior to encrypting. The same key must later be used to decrypt the value. See [[Setting the Encryption Key]].
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/config/has.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining If a Configuration Value Exists
3 | Topics: configuration
4 | Code: Config::get(), Config::has(), microtime()
5 | Id: 7
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to see if a configuration value exists.
11 |
12 | You know you can use a _default_ parameter to the `Config::get()` method, but you want to be able to determine if the configuration key is even present in your application's config.
13 | {/problem}
14 |
15 | {solution}
16 | Use `Config::has()`
17 |
18 | {php}
19 | if (Config::has('app.mykey'))
20 | {
21 | echo "Yea! 'mykey' is in config/app.php\n";
22 | }
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | It's interesting how this is implemented.
28 |
29 | The `Config::has()` method actually uses the `Config::get()` method with a default value of the current `microtime()`. It's only two lines of code.
30 |
31 | {php}
32 | $default = microtime(true);
33 | return $this->get($key, $default) !== $default;
34 | {/php}
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/app/missing.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering a 404 Error Handler
3 | Topics: -
4 | Code: App::missing()
5 | Id: 203
6 | Position: 21
7 | ---
8 |
9 | {problem}
10 | You want to trap any 404 errors you application generates.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::missing()` method.
15 |
16 | You'll place this code somewhere in your startup code. Usually `app/start/global.php`.
17 |
18 | {php}
19 | App::missing(function($exception)
20 | {
21 | return View::make('not-found')->withMessage($exception->getMessage());
22 | });
23 | {/php}
24 | {/solution}
25 |
26 | {discussion}
27 | Return a `Response` with your handler to finish the processing.
28 |
29 | In other words, if your missing handler returns anything, all further error handlers will be ignored and the response will be returned to the user.
30 |
31 | Sometimes, such as error logging, you don't want your handler to return anything. You may want it to do a bit of processing and allow other handlers to continue as normal.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/docs/recipes/form/get-select.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Select Option for a Value
3 | Topics: forms
4 | Code: Form::getSelectOption()
5 | Id: 180
6 | Position: 29
7 | ---
8 |
9 | {problem}
10 | You want to easily get the `` in this example.
27 |
28 | If the `$value` argument is an array, an option group will be returned.
29 | {/solution}
30 |
31 | {discussion}
32 | This is useful in Form macros.
33 |
34 | If you have a macro supplying a type of select box, this can be a handy method to call to build up your options.
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/form/old.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting a Value From the Session's Old Input
3 | Topics: forms
4 | Code: Form::old()
5 | Id: 176
6 | Position: 25
7 | ---
8 |
9 | {problem}
10 | You're redisplaying a page and want to get a field's value from the previously displayed page.
11 |
12 | In other words, you want to pull the value from the session's flashed data.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Form::old()` method.
17 |
18 | {php}
19 | $value = Form::old('fieldname');
20 | {/php}
21 |
22 | If there was no previous value, `null` is returned.
23 | {/solution}
24 |
25 | {discussion}
26 | Usually this is used in Form macros.
27 |
28 | And the most common reason you need to redisplay the same form is because there was an input error and you now want to display the form with an error message.
29 |
30 | But you want to keep any value that the user entered.
31 |
32 | This allows you to fetch that value if there was one.
33 |
34 | See also [[Getting the Value Attribute a Field Should Use]].
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/install/laravel.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing Laravel
3 | Topics: CodeIgniter, Composer, installation, Zend Framework
4 | Code: -
5 | Id: 22
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to install Laravel.
11 |
12 | You're ready to build beautiful web applications using PHP's best framework so you want to install Laravel and get going.
13 | {/problem}
14 |
15 | {solution}
16 | There is no solution.
17 | {/solution}
18 |
19 | {discussion}
20 | You do not install Laravel.
21 |
22 | Unlike [CodeIgniter](http://ellislab.com/codeigniter) or [Zend Framework](http://framework.zend.com/), you do not download and install Laravel.
23 |
24 | Instead, you create a new project and specify Laravel as one of the project's dependencies. This allows Composer to download and install the version of Laravel you want to use as part of your project.
25 |
26 | Laravel makes this process easy to accomplish with a single command. See [[Creating a Laravel Project]] for details on creating a new Laravel project.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/app/config/view.php:
--------------------------------------------------------------------------------
1 | array(__DIR__.'/../views'),
17 |
18 | /*
19 | |--------------------------------------------------------------------------
20 | | Pagination View
21 | |--------------------------------------------------------------------------
22 | |
23 | | This view will be used to render the pagination link output, and can
24 | | be easily customized here to show any view you like. A clean view
25 | | compatible with Twitter's Bootstrap is given to you by default.
26 | |
27 | */
28 |
29 | 'pagination' => 'pagination::slider-3',
30 |
31 | );
32 |
--------------------------------------------------------------------------------
/docs/recipes/form/get-id-attr.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the ID Attribute For a Field Name
3 | Topics: forms
4 | Code: Form::getIdAttribute(), Form::macro()
5 | Id: 174
6 | Position: 23
7 | ---
8 |
9 | {problem}
10 | You want to determine what the ID attribute is for a field name.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Form::getIdAttribute()` method.
15 |
16 | {php}
17 | $id = Form::getIdAttribute('fieldname', $attributes);
18 | {/php}
19 |
20 | This will return the ID or `null` if there is no ID for the field.
21 | {/solution}
22 |
23 | {discussion}
24 | This is often used in form macros.
25 |
26 | It's common to need to know the ID within a macro. If there's an explicit `'id'` key in your `$attributes` array it's easy, but Laravel can automatically generate ids based on labels within the form.
27 |
28 | The method also checks if there's any forms for the field name, returning the ID appropriately if that's the case.
29 |
30 | See the [[Creating Form Macros]] recipe information about form macros.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/app/views/partials/home-recipe.blade.php:
--------------------------------------------------------------------------------
1 | {{-- Displays a recipe's title and modification date or view count --}}
2 | {{-- Used to display latest and popular recipes on home page --}}
3 |
28 |
--------------------------------------------------------------------------------
/docs/recipes/app/bootcallbacks.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering Booting or Booted Callbacks
3 | Topics: callbacks
4 | Code: App::booted(), App::booting()
5 | Id: 103
6 | Position: 11
7 | ---
8 |
9 | {problem}
10 | You want something to happen during the boot process.
11 |
12 | You have a service provider you want to perform a specific task right before or right after the application is booted.
13 | {/problem}
14 |
15 | {solution}
16 | Use `App::booting()` or `App::booted()`
17 |
18 | {php}
19 | App::booted(function($app)
20 | {
21 | // Code to execute right after the app is booted.
22 | });
23 | App::booting(function($app)
24 | {
25 | // Code to execute right before the app is booted.
26 | })
27 | {/php}
28 | {/solution}
29 |
30 | {discussion}
31 | Understand where this happens in the request lifecycle.
32 |
33 | 1. First, all the service providers are booted.
34 | 2. Next, any `booting()` callbacks are called.
35 | 3. Finally, any `booted()` callbacks are called.
36 |
37 | See [[Understanding the Request Lifecycle]].
38 | {/discussion}
39 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/down.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Putting the Application in Maintenance Mode
3 | Topics: artisan
4 | Code: -
5 | Id: 61
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You need to put your application in "maintenance" mode.
11 |
12 | You're performing database updates, or other changes to your application and it'd be best if users couldn't access the system for a while.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan down` command.
17 |
18 | This sets a flag on your application specifying it's in maintenance mode.
19 |
20 | {php}
21 | $ php artisan down
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | Don't forget to handle maintenance mode.
27 |
28 | See the [[Registering a Maintenance Mode Handler]] recipe.
29 |
30 | What this command actually does is creates the empty file `app/storage/meta/down`. Existence of this file is the maintenance mode flag.
31 |
32 | If your application runs on several machines in a web farm then you must use the `php artisan down` command on each machine.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/file/get-require.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting the Return Value of a File.
3 | Topics: file system
4 | Code: File::get(), File::getRequire()
5 | Id: 128
6 | Position: 4
7 | ---
8 |
9 | {problem}
10 | You want to get the return value of a file.
11 |
12 | You have a PHP file that returns the value and want to execute the file and capture the return value.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `File::getRequire()` method.
17 |
18 |
19 | {php}
20 | 'value1',
24 | 'key2' => 'value2',
25 | );
26 | ?>
27 |
28 | // Fetching the array of the file above
29 | $value = File::getRequire('file1.php');
30 | {/php}
31 | {/solution}
32 |
33 | {discussion}
34 | If the file isn't found an exception is returned.
35 |
36 | Just like `File::get()` a `Illuminate\Filesystem\FileNotFoundException` exception is returned if the file is missing.
37 |
38 | It's a good idea to wrap this in a try/catch block unless you are certain the file exists.
39 | {/discussion}
40 |
--------------------------------------------------------------------------------
/docs/recipes/app/ready-response.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining if the Application is Ready for Responses
3 | Topics: -
4 | Code: App::isBooted(), App::readyForResponses()
5 | Id: 201
6 | Position: 19
7 | ---
8 |
9 | {problem}
10 | You want to know if your application is ready for responses.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::readyForResponses()` method.
15 |
16 | {php}
17 | // In a service provider
18 | if (App::readyForResponses())
19 | {
20 | // Take action when booted
21 | }
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | This is an alias to `App::isBooted()`.
27 |
28 | See [[Checking if the Application is Booted]].
29 |
30 | Since the application is ready for a response as soon as it's booted, and the application isn't booted until all the service providers are loaded, then any normal code in your application (`app/start/global.php`, controllers, routes, views) will always return a `true` result from this method.
31 |
32 | The only place that makes sense to call this is in service providers.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/auth/login-id.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Manually Logging a User by ID
3 | Topics: -
4 | Code: Auth::login(), Auth::loginUsingId()
5 | Id: 222
6 | Position: 27
7 | ---
8 |
9 | {problem}
10 | You want to manually log a user in from their ID.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::loginUsingId()` method.
15 |
16 | {php}
17 | $user = Auth::loginUsingId($user_id);
18 | if ( ! $user)
19 | {
20 | throw new Exception('Error logging in');
21 | }
22 | {/php}
23 |
24 | This will lookup the user from the `$user_id`, log them into the session, and return the user object. If `null` is returned, then you know the `$user_id` was invalid.
25 |
26 | If you want the "remember me" cookie to be set, pass `true` as the second argument.
27 |
28 | {php}
29 | Auth::loginUsingId($user_id, true);
30 | {/php}
31 | {/solution}
32 |
33 | {discussion}
34 | This does basically the same thing as `Auth::login()`.
35 |
36 | In fact, after this method looks up the user it calls `Auth::login()`. See [[Manually Logging a User In]] for more details.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/config/has-group.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Determining If a Configuration Group Exists
3 | Topics: configuration, environment
4 | Code: Config::hasGroup()
5 | Id: 8
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You want to see if a configuration group exists.
11 |
12 | You know Laravel organizes the configuration into groups and these groups are implemented as individual config files. For instance the `app/config/database.php` contains configuration for the _database_ group.
13 |
14 | You want to see if the group even exists.
15 | {/problem}
16 |
17 | {solution}
18 | Use `Config::hasGroup()`.
19 |
20 | {php}
21 | if (Config::hasGroup('session'))
22 | {
23 | echo "I have a config/session.php file!";
24 | }
25 | {/php}
26 | {/solution}
27 |
28 | {discussion}
29 | This only works for groups existing in the production environment.
30 |
31 | In other words, if your environment is _local_ and you have a `app/config/local/shoes.php` file, but no top level `app/config/shoes.php` then the `Config::hasGroup("shoes")` statement will return `false`.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/app/views/partials/popular-items.blade.php:
--------------------------------------------------------------------------------
1 | {{-- Displays a category and the top three recipes for the category. Used --}}
2 | {{-- to display popular categories/recipes in the middle of the home page --}}
3 |
27 |
--------------------------------------------------------------------------------
/docs/recipes/blade/set-tags2.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Escaped Content Tags Blade Uses
3 | Topics: -
4 | Code: Blade::setEscapedContentTags()
5 | Id: 247
6 | Position: 19
7 | ---
8 |
9 | {problem}
10 | You want to use different escaped content tags in your Blade template.
11 |
12 | You know that blade uses `{{{` and `}}}` to specify content to be escaped and output, but you want to use something different.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Blade::setEscapedContentTags()` method.
17 |
18 | Let's say you want to use `[{{` and `}}]` for your tags. First call the method.
19 |
20 | {php}
21 | Blade::setContentTags('[{{', '}}]');
22 | {/php}
23 |
24 | Then your template can contain code like.
25 |
26 | {html}
27 | The value of $variable is [{{ $variable }}].
28 | {/html}
29 |
30 | `$variable` will pass through `HTML::entities()` before being output.
31 | {/solution}
32 |
33 | {discussion}
34 | This actually calls `Blade::setContentTags()`.
35 |
36 | It passes the third argument as `true`. See [[Setting the Content Tags Blade Uses]].
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/controller/get-filters.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Retrieving a Controller's Before and After Filters
3 | Topics: filters
4 | Code: Controller::getAfterFilters(), Controller::getBeforeFilters()
5 | Id: 47
6 | Position: 3
7 | ---
8 |
9 | {problem}
10 | You need to access the controller's filters.
11 | {/problem}
12 |
13 | {solution}
14 | Use `getAfterFilters()` or `getBeforeFilters()` in the controller.
15 |
16 | {php}
17 | class SomeController extends Controller {
18 | public function someMethod()
19 | {
20 | // Dump all the before filters
21 | var_dump($this->getBeforeFilters());
22 | }
23 | }
24 | {/php}
25 | {/solution}
26 |
27 | {discussion}
28 | This is a lower level function.
29 |
30 | Generally, you shouldn't need to access the filters within a controller method. The before filters already have ran successfully if you're in any method other than `__construct()`.
31 |
32 | The after filters won't fire until after the controller's method returns.
33 |
34 | But if you need to access them, this recipe describes how.
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/app/filters.php:
--------------------------------------------------------------------------------
1 | path())) !== null)
14 | {
15 | return $content;
16 | }
17 | });
18 |
19 | /*
20 | |--------------------------------------------------------------------------
21 | | Cache After Filter
22 | |--------------------------------------------------------------------------
23 | |
24 | | Saves the response in the cache for next time
25 | |
26 | */
27 | Route::filter('cache_set', function($route, $request, $response = null)
28 | {
29 | if ($response instanceof Illuminate\Http\Response)
30 | {
31 | if (($content = PageCache::set($request->path(), $response->getContent())))
32 | {
33 | $response->setContent($content);
34 | }
35 | }
36 | });
37 |
--------------------------------------------------------------------------------
/docs/recipes/app/isdown.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Checking if the Application is Down for Maintenance
3 | Topics: -
4 | Code: App::isDownForMaintenance()
5 | Id: 120
6 | Position: 14
7 | ---
8 |
9 | {problem}
10 | You want to check if your application is down for maintenance.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::isDownForMaintenance()` method.
15 |
16 | {php}
17 | if (App::isDownForMaintenance())
18 | {
19 | die("We're currently down for maintenance.");
20 | }
21 | {/php}
22 | {/solution}
23 |
24 | {discussion}
25 | There's a better way to do this.
26 |
27 | See the [[Registering a Maintenance Mode Handler]] recipe. This will allow you to set up an action to occur automatically when you're application is in maintenance mode.
28 |
29 | Note that your application is considered in maintenance mode of the file `app/storage/meta/down` exists on your file system.
30 |
31 | See also:
32 |
33 | * [[Registering a Maintenance Mode Handler]]
34 | * [[Putting the Application in Maintenance Mode]]
35 | * [[Bringing the Application Out of Maintenance Mode]]
36 | {/discussion}
37 |
--------------------------------------------------------------------------------
/docs/recipes/html/link-sasset.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Generating a Secure HTML Link to an Asset
3 | Topics: html
4 | Code: HTML::linkAsset(), HTML::linkSecureAsset(), HTML::secureLink()
5 | Id: 189
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You want to use a HTTPS link to an asset within your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `HTML::linkSecureAsset()` method.
15 |
16 | This method takes the exact arguments as `HTML::secureLink()` (see [[Generating a Secure HTML Link]]).
17 | {/solution}
18 |
19 | {discussion}
20 | There's a small difference between links to web pages and links to assets.
21 |
22 | Depending on your application's configuration, your web pages may have `index.php` at the start of the path. Assets never do.
23 |
24 | When determining the URL for the asset, `HTML::linkSecureAsset()` will strip away any `index.php` if it's there.
25 |
26 | This method is a wrapper for `HTML::linkAsset()`, but it always passes `true` as the fourth argument to `HTML::linkAsset()`. See [[Generating a HTML Link to an Asset]] for details.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/docs/recipes/auth/login.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Manually Logging a User In
3 | Topics: -
4 | Code: Auth::attempt(), Auth::login()
5 | Id: 221
6 | Position: 26
7 | ---
8 |
9 | {problem}
10 | You want to manually log a user in.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Auth::login()` method.
15 |
16 | This logs the user into the current session, bypassing any authentication.
17 |
18 | {php}
19 | $user = User::find($user_id);
20 | Auth::login($user);
21 | {/php}
22 |
23 | You can also specify `true` as the second parameter to set the "remember me" cookie.
24 |
25 | {php}
26 | Auth::login($user, true);
27 | {/php}
28 | {/solution}
29 |
30 | {discussion}
31 | The `Auth::attempt()` method does this automatically.
32 |
33 | In face, if the authentication is successful, `Auth::attempt()` will call `Auth::login()` internally.
34 |
35 | But, if you need to bypass authentication altogether, this is a great method to use. It's especially useful when testing: just create a temporary route to log a user in using this method.
36 |
37 | This will fire the `auth.login` event.
38 | {/discussion}
39 |
--------------------------------------------------------------------------------
/docs/recipes/help/community.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: The Laravel Community
3 | Topics: community, forums, help, IRC
4 | Code: -
5 | Id: 14
6 | Position: 2
7 | ---
8 |
9 | {problem}
10 | You need additional help.
11 |
12 | You ran into a problem trying to do something in Laravel and cannot find the answer in any of the documentation sources.
13 | {/problem}
14 |
15 | {solution}
16 | Ask the Laravel Community for help.
17 |
18 | Join the **#laravel** channel using IRC. If you don't have an IRC client, try the [Laravel Web IRC Client](http://laravel.io/irc).
19 |
20 | You also can post your question on the [Laravel Forums](http://forums.laravel.io).
21 | {/solution}
22 |
23 | {discussion}
24 | The quickest way to receive help is through IRC.
25 |
26 | There are always dozens of people logged in, eager to help. Often you'll be asked to show a snippet of the code you're having troubles with. Try paring down your code to the minimum representation of the problem and paste it at [paste.laravel.com](http://paste.laravel.com/). This provides a quick way to share your code so others can help.
27 | {/discussion}
28 |
--------------------------------------------------------------------------------
/app/Recipes/Category.php:
--------------------------------------------------------------------------------
1 | belongsTo('Section');
8 | }
9 |
10 | public function recipes()
11 | {
12 | return $this->hasMany('Recipe');
13 | }
14 |
15 | /**
16 | * Synchronize all section ids by the slugs
17 | */
18 | public static function syncSections($section_id, array $slugs)
19 | {
20 | $ids_to_add = static::whereIn('slug', $slugs)
21 | ->where('section_id', '<>', $section_id)
22 | ->lists('id');
23 | $ids_to_del = static::whereNotIn('slug', $slugs)
24 | ->where('section_id', '=', $section_id)
25 | ->lists('id');
26 | if (count($ids_to_add))
27 | {
28 | static::whereIn('id', $ids_to_add)
29 | ->update(['section_id' => $section_id]);
30 | }
31 | if (count($ids_to_del))
32 | {
33 | static::whereIn('id', $ids_to_del)
34 | ->update(['section_id' => 0]);
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/app/views/code.blade.php:
--------------------------------------------------------------------------------
1 | @extends('layouts.default')
2 |
3 | @section('title')
4 | @parent :: {{{ $code->name }}} Code
5 | @stop
6 |
7 | @section('breadcrumbs')
8 |
9 |
35 |
36 | @stop
37 |
--------------------------------------------------------------------------------
/docs/recipes/lang/set-select.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting the Translation Message Selector
3 | Topics: -
4 | Code: Lang::setSelector()
5 | Id: 260
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You want to change how the message selector logic works.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Lang::setSelector()` to set your own message selector.
15 |
16 | {php}
17 | $selector = new MyMessageSelector;
18 | Lang::setSelector($selector);
19 | {/php}
20 |
21 | The message select is what handles pulling the correct message from a pipe (|) delimited string based on a number, locale, and the pluralizaion rules in the message itself.
22 |
23 | Your class must extend the `Symfony\Component\Translation\MessageSelector` class. It should then override the `choose()` method to implement your custom logic.
24 |
25 | See also the [[Getting the Translation Message Selector]] recipe.
26 | {/solution}
27 |
28 | {discussion}
29 | Where to call this method?
30 |
31 | It should be called earlier than the `Lang` facade is used. Since this is very low level coding, the best place is a service provider.
32 | {/discussion}
33 |
--------------------------------------------------------------------------------
/app/views/topic.blade.php:
--------------------------------------------------------------------------------
1 | @extends('layouts.default')
2 |
3 | @section('title')
4 | @parent :: {{{ $topic->name }}} Topic
5 | @stop
6 |
7 | @section('breadcrumbs')
8 |
9 |
35 |
36 | @stop
37 |
--------------------------------------------------------------------------------
/docs/recipes/blade/foreach.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Using @foreach Control Structures in Blade
3 | Topics: Blade
4 | Code: @endforeach, @foreach
5 | Id: 88
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You want to loop through an array in your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `@foreach` control structure.
15 |
16 | {html}
17 |
18 |
19 |
A list of items.
20 |
21 | @foreach ($items as $item)
22 |
{{{ $item }}
23 | @endforeach
24 |
25 |
26 |
27 | {/html}
28 |
29 | Just like PHP, you can loop with the key.
30 |
31 | {html}
32 |
33 |
34 |
A dictionary.
35 |
36 | @foreach ($dict as $word => $meaning)
37 |
{{{ $word }}}
38 |
{{{ $meaning }}
39 | @endforeach
40 |
41 |
42 |
43 | {/html}
44 | {/solution}
45 |
46 | {discussion}
47 | Beware undefined variables.
48 |
49 | Just like PHP, if either `$items` (in the first example) or `$dict` (in the second example) are not defined, a warning message will be output.
50 | {/discussion}
51 |
--------------------------------------------------------------------------------
/app/database/migrations/2013_11_14_102138_create_recipe_table.php:
--------------------------------------------------------------------------------
1 | increments('id');
18 | $table->string('title')->unique();
19 | $table->integer('category_id')->unsigned()->index();
20 | $table->text('problem');
21 | $table->text('solution');
22 | $table->text('discussion');
23 | $table->integer('position');
24 | $table->integer('views');
25 | $table->timestamps();
26 | $table->engine = 'MyISAM';
27 | });
28 | }
29 |
30 | /**
31 | * Reverse the migrations.
32 | *
33 | * @return void
34 | */
35 | public function down()
36 | {
37 | Schema::drop('recipes');
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/docs/recipes/cookie/make.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating a new Cookie
3 | Topics: cookies
4 | Code: Cookie::make()
5 | Id: 48
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to create a cookie.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Cookie::make()`
15 |
16 | You can use this method to generate a new cookie. Keep in mind, this does not send a cookie to the user, it simply creates a cookie object.
17 |
18 | To create a simple cookie, which will expire at the end of the user's session.
19 |
20 | {php}
21 | $cookie = Cookie::make($name, $value);
22 | {/php}
23 |
24 | To create a cookie that won't expire for 60 minutes, add a third parameters.
25 |
26 | {php}
27 | $cookie = Cookie::make($name, $value, 60);
28 | {/php}
29 |
30 | Of course, you can pass all the normal cookie parameters.
31 |
32 | {php}
33 | $cookie = Cookie::make($name, $value, $minutes, $path, $domain,
34 | $secure, $httpOnly);
35 | {/php}
36 | {/solution}
37 |
38 | {discussion}
39 | What should you do with the cookie?
40 |
41 | Well, normally, you'd send it the user. Additional recipes coming for this.
42 | {/discussion}
43 |
--------------------------------------------------------------------------------
/docs/recipes/form/submit.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Creating a Submit Button
3 | Topics: forms
4 | Code: Form::submit()
5 | Id: 171
6 | Position: 21
7 | ---
8 |
9 | {problem}
10 | You want to create a submit button in your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `Form::submit()` method.
15 |
16 | You don't have to pass any arguments.
17 |
18 | {html}
19 | {{ Form::submit() }}
20 | {/html}
21 |
22 | The resulting HTML will be as follows.
23 |
24 | {html}
25 |
26 | {/html}
27 |
28 | You can specify the value as the first argument.
29 |
30 | {html}
31 | {{ Form::submit('Save') }}
32 | {/html}
33 |
34 | Now the resulting HTML has a value.
35 |
36 | {html}
37 |
38 | {/html}
39 |
40 | Use the second argument to add additional attributes.
41 |
42 | {html}
43 | {{ Form::submit('Save', array('class' => 'btn')) }}
44 | {/html}
45 |
46 | And now resulting the submit button has a class.
47 |
48 | {html}
49 |
50 | {/html}
51 | {/solution}
52 |
53 | {discussion}
54 | Nothing to discuss.
55 | {/discussion}
56 |
--------------------------------------------------------------------------------
/docs/recipes/app/down.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering a Maintenance Mode Handler
3 | Topics: -
4 | Code: App::down(), App::isDownForMaintenance()
5 | Id: 121
6 | Position: 15
7 | ---
8 |
9 | {problem}
10 | You want code to automatically execute when your application is down.
11 |
12 | You know you can use the `App::isDownForMaintenance()` method, but you'd like your application to take appropriate action at the appropriate time when it's in maintenance mode.
13 | {/problem}
14 |
15 | {solution}
16 | Register a handler with `App::down()`.
17 |
18 | {php}
19 | App::down(function()
20 | {
21 | return Response::view('maintenance.mode', array(), 503);
22 | });
23 | {/php}
24 |
25 | That snippet of code will output the `maintenance.mode` view with a HTTP status code of 503.
26 | {/solution}
27 |
28 | {discussion}
29 | The first thing your application does after loading and booting is execute your down handler if needed.
30 |
31 | Yo can see where this occurs in [[Understanding the Request Lifecycle]]. Look at the **Running Steps**.
32 |
33 | See also [[Checking if the Application is Down for Maintenance]].
34 | {/discussion}
35 |
--------------------------------------------------------------------------------
/docs/recipes/install/xcache.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Installing XCache
3 | Topics: cache, installation, XCache
4 | Code: Cache
5 | Id: 99
6 | Position: 13
7 | ---
8 |
9 | {problem}
10 | You want to speed up your application using a cache.
11 | {/problem}
12 |
13 | {solution}
14 | Install XCache
15 |
16 | #### Step 1 - Install XCache
17 |
18 | {bash}
19 | $ sudo apt-get install php5-xcache
20 | {/bash}
21 |
22 | #### Step 2 - Edit xcache.ini
23 |
24 | To use the variable cache you need to edit the `xcache.var_size` setting in the ini file. Usually, this file is located in `/etc/php5/mods-available`
25 |
26 | {text}
27 | # Find the line
28 | xcache.var_size = 0M
29 |
30 | # Change it to
31 | xcache.var_size = 100M
32 | {/text}
33 |
34 | #### Step 3 - Restart apache
35 |
36 | {bash}
37 | $ sudo service apache2 restart
38 | {/bash}
39 | {/solution}
40 |
41 | {discussion}
42 | XCache is a fast, stable PHP opcode cacher.
43 |
44 | It also provides standard get/set/inc/dec cache operations which Laravel can use to drive its `Cache` component.
45 |
46 | See [[Setting up the XCache Cache Driver]] for instructions.
47 | {/discussion}
48 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/env.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Displaying the Current Environment
3 | Topics: artisan, environment
4 | Code: -
5 | Id: 6
6 | Position: 5
7 | ---
8 |
9 | {problem}
10 | You're not sure what the console environment is.
11 |
12 | Your web page is appearing correctly, but you suspect the environment the console is using is different that the web.
13 | {/problem}
14 |
15 | {solution}
16 | Use `php artisan env`
17 |
18 | {text}
19 | $ php artisan env
20 | Current application environment: production
21 | {/text}
22 | {/solution}
23 |
24 | {discussion}
25 | Be careful with your environment detections.
26 |
27 | The environment setting allows you to set up custom configurations for different machines. Most often this is used to configure database settings and API access points differently between production and development. If you do not pay close attention to your environment detection the situation can arise where web requests operate with one environment setting and console requests use a different one.
28 |
29 | See [[Checking Your Environment]] for instructions using PHP Code to determine your environment.
30 | {/discussion}
31 |
--------------------------------------------------------------------------------
/docs/recipes/app/is-booted.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Checking if the Application is Booted
3 | Topics: -
4 | Code: App::isBooted()
5 | Id: 57
6 | Position: 10
7 | ---
8 |
9 | {problem}
10 | You want to know if your application is booted.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::isBooted()` method.
15 |
16 | {php}
17 | // Usually in a service provider
18 | if (App::isBooted())
19 | {
20 | // Take action when booted
21 | }
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | Booting occurs at a low level.
27 |
28 | Your application is "booted" before the request is even dispatched. It is booted before `app/start/global.php` or `app/routes.php` or `app/filters.php` is loaded. _(These files are loaded within a "booted" callback.)_
29 |
30 | Booting the application occurs internally and consists of three steps:
31 |
32 | 1. Call any registered "booting" callbacks.
33 | 2. Flag the application as booted.
34 | 3. Call any registered "booted" callbacks.
35 |
36 | Therefore, the most likely places you'll use `App::isBooted()` are within any registered "booting" or "booted" callbacks. Or within a service provider.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/tail.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Tailing a Log File on a Remote Server
3 | Topics: artisan
4 | Code: -
5 | Id: 281
6 | Position: 28
7 | ---
8 |
9 | {problem}
10 | You want to see the contents of your application's log file.
11 |
12 | You'd like to view the log in real-time, seeing new entries as they occur.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan tail` command.
17 |
18 | Using the command by itself will display the contents of `app/storage/logs/laravel.log` in your local application.
19 |
20 | {bash}
21 | $ php artisan tail
22 | ...
23 | log contents
24 | ...
25 | {/bash}
26 |
27 | Press `Ctrl+C` to exit the log tail.
28 |
29 | If you set up a remote connection in `app/config/remote.php`, you can use the tail command to view the log file on the remote machine.
30 |
31 | {bash}
32 | $ php artisan tail remote-name
33 | ...
34 | log contents
35 | ...
36 | {/bash}
37 |
38 | Press `Ctrl+C` to exit the log tail.
39 | {/solution}
40 |
41 | {discussion}
42 | For non-standard log files use the `--path` option.
43 |
44 | {bash}
45 | $ php artisan tail --path=/full/path/to/log.file
46 | {/bash}
47 | {/discussion}
48 |
--------------------------------------------------------------------------------
/docs/recipes/cache/remember.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Retrieving an Item from the Cache or Storing a Default
3 | Topics: cache
4 | Code: Cache::put(), Cache::remember()
5 | Id: 268
6 | Position: 17
7 | ---
8 |
9 | {problem}
10 | You want to retrieve an item from the cache.
11 |
12 | But, if it doesn't exist you want to store the value in the cache.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Cache::remember()` value.
17 |
18 | {php}
19 | $value = Cache::remember($key, $minutes, function()
20 | {
21 | // fetch value from db or some other logic
22 | return $value;
23 | });
24 | {/php}
25 |
26 | If the item exists in the cache, it's returned immediately. But if it doesn't exist then the function is executed and the return value of the function is cached the specified minutes. In the later case this value is also returned.
27 | {/solution}
28 |
29 | {discussion}
30 | The `Cache::remember()` method is a nice all-in-one shortcut.
31 |
32 | It implements a workflow similar to the one described in the discussion of [[Storing an Item in the Cache]]. The function won't be executed unless the value isn't yet in the cache.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/cache/sections.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Organizing Your Cache into Sections
3 | Topics: cache
4 | Code: Cache::section()
5 | Id: 101
6 | Position: 9
7 | ---
8 |
9 | {problem}
10 | You have many items in your cache and want to organize them.
11 | {/problem}
12 |
13 | {solution}
14 | Organize your cache into sections.
15 |
16 | You can use the `Cache::section()` method to specify categories or _groups_ of cache keys.
17 |
18 | {php}
19 | $item = Cache::section('inventory')->get('last-purchased');
20 | {/php}
21 |
22 | The nice thing about sections is you can treat the entire section as sort of a "mini-cache" and use all the cache methods on just that section.
23 |
24 | {php}
25 | // Store a value
26 | Cache::section('section')->put('key', 'value', $minutes);
27 |
28 | // Retrieve a value
29 | $value = Cache::section('section')->get('key');
30 |
31 | // Flush the whole section
32 | Cache::section('section')->flush();
33 | {/php}
34 | {/solution}
35 |
36 | {discussion}
37 | Sections aren't available for every Cache driver.
38 |
39 | Neither the File Cache driver nor the Database Cache driver supports cache sections.
40 | {/discussion}
41 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/migrate-rollback.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Rolling Back the Last Database Migration
3 | Topics: artisan, migrations
4 | Code: -
5 | Id: 66
6 | Position: 14
7 | ---
8 |
9 | {problem}
10 | You want to "undo" your last database migration.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `php artisan migrate:rollback` command.
15 |
16 | {php}
17 | $ php artisan migrate:rollback
18 | {/php}
19 |
20 | The command will list the migrations which have been undone.
21 |
22 | To see what rollback will do, use the `--pretend` option.
23 |
24 | {php}
25 | $ php artisan migrate:rollback --pretend
26 | {/php}
27 |
28 | You can also specify a database connection other than the default one.
29 |
30 | {php}
31 | $ php artisan migrate:rollback --pretend --database=other-one
32 | {/php}
33 | {/solution}
34 |
35 | {discussion}
36 | This command will undo the last "set" of migrations.
37 |
38 | If the previous time you ran `php artisan migrate` ended up performing three different migrations, then `php artisan migrate:rollback` will undo those three migrations.
39 |
40 | This encourages small, incremental changes to your database.
41 | {/discussion}
42 |
--------------------------------------------------------------------------------
/docs/recipes/cache/memcached.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting up the Memcached Cache Driver
3 | Topics: cache, configuration, memcached
4 | Code: -
5 | Id: 94
6 | Position: 4
7 | ---
8 |
9 | {problem}
10 | You want to speed up Laravel's cache.
11 |
12 | You know by default Laravel uses the file cache driver. You want to use a speedier cache.
13 | {/problem}
14 |
15 | {solution}
16 | Use the Memcached cache driver.
17 |
18 | Edit `app/config/cache.php` and change the driver to `'memcached'`.
19 |
20 | {php}
21 | 'driver' => 'memcached',
22 | {/php}
23 |
24 | If you have multiple memcached servers, or they're running on something other than the local machine, you'll have to edit the **memcached** section of `app/config/cache.php` also.
25 |
26 | {php}
27 | 'memcached' => array(
28 | array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100),
29 | ),
30 | {/php}
31 | {/solution}
32 |
33 | {discussion}
34 | Memcached is a free, high-performance, distributed memory object caching system.
35 |
36 | To use the cache driver make sure Memcached is installed first. See the [[Installing Memcached]] recipe for details.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/auth/changing-model.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Changing Your Authentication Model
3 | Topics: authentication, configuration
4 | Code: config/auth.php, RemindableInterface, UserInterface
5 | Id: 11
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You need the change the authentication model from the default `User`.
11 |
12 | Your application is using namespaces or you want to use a differently named model for users.
13 | {/problem}
14 |
15 | {solution}
16 | Edit `app/config/auth.php` to change the model.
17 |
18 | {php}
19 | 'model' => 'MyApp\Models\User',
20 | {/php}
21 | {/solution}
22 |
23 | {discussion}
24 | Don't forget the required interfaces.
25 |
26 | If you're using your own model it's important that your model implements Auth's `UserInterface`. If you're implementing the password reminder feature it should also implement `RemindableInterface`.
27 |
28 | {php}
29 | name";
21 | }
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | What does `Auth::user()` return?
27 |
28 | The method returns one of three values.
29 |
30 | `null` is always returned if there is no current user (aka no user logged in).
31 |
32 | The other return value is based on your authentication configuration. If your driver is **eloquent** then the object return is the class specified by the the model setting.
33 |
34 | See [[Changing Your Authentication Driver]] for information about the driver setting.
35 |
36 | See [[Changing Your Authentication Model]] for information about the model setting.
37 |
38 | If your authentication driver is **database** then the object returned is a generic user. Specifically it's the `Illuminate\Auth\GenericUser` class.
39 | {/discussion}
40 |
--------------------------------------------------------------------------------
/docs/recipes/cache/file.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting Up the File Cache Driver
3 | Topics: configuration
4 | Code: Cache::decrement(), Cache::increment()
5 | Id: 37
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to use the File driver for the Laravel Cache.
11 |
12 | You know other caches are more efficient than using the file system, but you want to get going quickly.
13 | {/problem}
14 |
15 | {solution}
16 | It should be pre-configured.
17 |
18 | Laravel's default configuration uses the file cache driver. You can check the configuration by examining `app/config/cache.php`. Look for the **driver** setting. It should look like what's below.
19 |
20 | {php}
21 | 'driver' => 'file',
22 | {/php}
23 |
24 | If the setting is anything other than **file**, you can change it here. Or, if you're using [[Environment Specific Configurations]], you'll want to change this setting in your `app/config/{envname}/cache.php` file.
25 | {/solution}
26 |
27 | {discussion}
28 | The file cache driver has some limitations.
29 |
30 | Specifically, you cannot use the `Cache::increment()` and `Cache::decrement()` methods. And Cache Sections are not supported.
31 | {/discussion}
32 |
--------------------------------------------------------------------------------
/docs/recipes/cache/array.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting up the Array Cache Driver
3 | Topics: cache, configuration
4 | Code: -
5 | Id: 97
6 | Position: 6
7 | ---
8 |
9 | {problem}
10 | You want to disable caching.
11 |
12 | You don't want to go through your code and change all the places the cache is used, but you don't want any cached values to be retained between requests.
13 | {/problem}
14 |
15 | {solution}
16 | Use the Array cache driver.
17 |
18 | Edit `app/config/cache.php` and change the driver to `'array'`.
19 |
20 | {php}
21 | 'driver' => 'array',
22 | {/php}
23 | {/solution}
24 |
25 | {discussion}
26 | This is what the testing configuration does.
27 |
28 | Laravel provides a `app/config/test/cache.php` file which overrides the default cache setting to use the array driver.
29 |
30 | The array driver simply stores any cached values in an internal array. The values are not retained between requests.
31 |
32 | {warn}
33 | Cached values are available within the same request. If you have a `Cache::put('mykey')` at one place in your code and later, in the same request, have `Cache::get('mykey')` the value will be available.
34 | {/warn}
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/app/routes.php:
--------------------------------------------------------------------------------
1 | 'cache_get', 'after' => 'cache_set'], function()
9 | {
10 | Route::get('/', 'PageController@home');
11 | Route::get('contents', 'PageController@contents');
12 | Route::get('faq', 'PageController@faq');
13 | Route::get('topics', 'PageController@topics');
14 | Route::get('codes', 'PageController@codes');
15 | Route::get('categories/{category}', 'PageController@category');
16 | Route::get('topics/{topic}', 'PageController@topic');
17 | Route::get('codes/{code}', 'PageController@code');
18 | });
19 |
20 | // Recipe pages are cachable, but the controller method handles it
21 | Route::get('recipes/{recipe}/{slug?}', 'PageController@recipe');
22 |
23 | // Search function isn't cachable
24 | Route::get('search', [
25 | 'as' => 'search',
26 | 'uses' => 'Recipes\Controllers\SearchController@go',
27 | ]);
28 | Route::get('search/terms', [
29 | 'as' => 'search.terms',
30 | 'uses' => 'Recipes\Controllers\SearchController@terms',
31 | ]);
32 |
--------------------------------------------------------------------------------
/docs/recipes/html/entities.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Converting a HTML String to Entities
3 | Topics: html
4 | Code: e(), HTML::entities(), htmlentities()
5 | Id: 122
6 | Position: 1
7 | ---
8 |
9 | {problem}
10 | You want to _"escape"_ html in your web page output.
11 |
12 | You know you can use the PHP `htmlentities()` method, but want to do it the Laravel way.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `HTML::entities()` method.
17 |
18 | {php}
19 | echo HTML::entities('
Title example
');
20 | {/php}
21 |
22 | The above will convert the less than symbols to < and the greater than symbols to >
23 |
24 | You can also use the helper function `e()`.
25 |
26 | {php}
27 | echo e('
Title example
');
28 | {/php}
29 |
30 | The above will produce the same output as the longer `HTML::entities()` method.
31 | {/solution}
32 |
33 | {discussion}
34 | This method actually calls `htmlentities()`.
35 |
36 | Specifically it calls `htmlentities($your_string, ENT_QUOTES, 'UTF-8', false)`.
37 |
38 | This will convert quotes (both single and double), use UTF-8 as the character encoding and won't convert entities in the string already converted.
39 | {/discussion}
40 |
--------------------------------------------------------------------------------
/app/views/category.blade.php:
--------------------------------------------------------------------------------
1 | @extends('layouts.default')
2 |
3 | @section('title')
4 | @parent :: {{{ $category->name }}} Category
5 | @stop
6 |
7 | @section('breadcrumbs')
8 |
9 |
36 |
37 | @stop
38 |
--------------------------------------------------------------------------------
/docs/recipes/artisan/migrate-refresh.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Resetting and Re-running All Migrations
3 | Topics: artisan, migrations
4 | Code: -
5 | Id: 68
6 | Position: 16
7 | ---
8 |
9 | {problem}
10 | You want to reset and re-run all your migrations.
11 |
12 | Maybe you've made some database changes by hand. You want to get your database to the exact structure a fresh install would have.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `php artisan migrate:refresh` command.
17 |
18 | {php}
19 | $ php artisan migrate:refresh
20 | {/php}
21 |
22 | To reseed the database when complete, you can use the `--seed` option.
23 |
24 | {php}
25 | $ php artisan migrate:refresh --seed
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | This command saves a few steps.
31 |
32 | Instead of issuing ...
33 |
34 | {php}
35 | $ php artisan migrate:reset
36 | $ php artisan migrate
37 | {/php}
38 |
39 | This one command combines both functions.
40 |
41 | {tip}
42 | **Something to keep in mind.** When you clear all your migrations and run migrations again this creates a new migration set. A subsequent `migrate:rollback` will rollback all migrations performed in this set.
43 | {/tip}
44 | {/discussion}
45 |
--------------------------------------------------------------------------------
/docs/recipes/cache/redis.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Setting up the Redis Cache Driver
3 | Topics: cache, configuration, redis
4 | Code: -
5 | Id: 96
6 | Position: 5
7 | ---
8 |
9 | {problem}
10 | You want to speed up Laravel's cache.
11 |
12 | You know by default Laravel uses the file cache driver. You want to use a speedier cache.
13 | {/problem}
14 |
15 | {solution}
16 | Use the Redis cache driver.
17 |
18 | Edit `app/config/cache.php` and change the driver to `'redis'`.
19 |
20 | {php}
21 | 'driver' => 'redis',
22 | {/php}
23 |
24 | If you have multiple redis servers, or they're running on something other than the local machine, you'll have to edit the **redis** section of `app/config/database.php` also.
25 |
26 | {php}
27 | 'redis' => array(
28 | 'cluster' => false,
29 | 'default' => array(
30 | 'host' => '127.0.0.1',
31 | 'port' => 6379,
32 | 'database' => 0,
33 | ),
34 | ),
35 | {/php}
36 | {/solution}
37 |
38 | {discussion}
39 | Redis is a free, advanced key-value store.
40 |
41 | To use the cache driver make sure Redis is installed first. See the [[Installing Redis]] recipe for details.
42 | {/discussion}
43 |
--------------------------------------------------------------------------------
/docs/recipes/configuration/env-closure.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Detecting the Environment with a Closure
3 | Topics: configuration, environment
4 | Code: App::detectEnvironment()
5 | Id: 28
6 | Position: 5
7 | ---
8 |
9 | {problem}
10 | Determining your environment is complicated.
11 |
12 | You need more flexibility than simply checking hostnames to determine the environment for your Laravel application.
13 | {/problem}
14 |
15 | {solution}
16 | Use a Closure to determine your environment.
17 |
18 | Edit `bootstrap/start.php` and use a Closure to do the detecting.
19 |
20 | {php}
21 | $env = $app->detectEnvironment(function()
22 | {
23 | return getenv('_MY_ENVIRONMENT');
24 | });
25 | {/php}
26 | {/solution}
27 |
28 | {discussion}
29 | There's multiple ways to detect your environment.
30 |
31 | The `$app->detectEnvironment()` method can take either an array _(as illustrated in the [[Environment Specific Configurations]] recipe)_ or a `Closure`.
32 |
33 | When an array of `'env' => array('hostnames')` is provided, if no match is found then the environment defaults to "production".
34 |
35 | But if a `Closure` is provided then it must return something. There is no default.
36 | {/discussion}
37 |
--------------------------------------------------------------------------------
/docs/recipes/app/finish.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering a Finish Callback
3 | Topics: -
4 | Code: App::finish()
5 | Id: 200
6 | Position: 18
7 | ---
8 |
9 | {problem}
10 | You want to have code execute after the request is sent to the user, but before the application terminates.
11 | {/problem}
12 |
13 | {solution}
14 | Use the `App::finish()` method to register a finish callback.
15 |
16 | {php}
17 | App::finish(function($request, $response)
18 | {
19 | // Use request and/or response to do logging or some after
20 | // main processing stuff
21 | });
22 | {/php}
23 |
24 | **NOTE:** Although `$request` and `$response` are provided to the callback, modification of either of these will have no affect in the application.
25 | {/solution}
26 |
27 | {discussion}
28 | Where do you register the callbacks?
29 |
30 | You can put the `App::finish()` call in a service provider or even `app/start/global.php`. Since any callbacks are not called until late in the process the question _"Where to put them?"_ doesn't matter too much.
31 |
32 | Look at [[Understanding the Request Lifecycle]]. The second to last step in the section labeled **The Running Steps** shows when this is called.
33 | {/discussion}
34 |
--------------------------------------------------------------------------------
/docs/recipes/blade/choice.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Outputting a Translation With Pluralization in a Blade Template
3 | Topics: localization
4 | Code: @choice, Lang::choice()
5 | Id: 255
6 | Position: 21
7 | ---
8 |
9 | {problem}
10 | You want to use a translation with pluralization rules in your Blade template.
11 | {/problem}
12 |
13 | {solution}
14 | Use the Blade `@choice` command.
15 |
16 | For example:
17 |
18 | {html}
19 | @choice('messages.items', 1)
20 | {/html}
21 |
22 | This would output the message from your `messages.php` language file using the `items` key when the count is 1.
23 |
24 | If your message has placeholders, you can specify them with an additional array argument.
25 |
26 | {html}
27 | @choice('message.items', 3, ['type' => 'widget']);
28 | {/html}
29 |
30 | This would replace any `:type` in the message with the word `widget`. Note that the `:count` placeholder is automatically set.
31 | {/solution}
32 |
33 | {discussion}
34 | See the `Lang::choice()` method.
35 |
36 | The recipe [[Getting a Translation Using Pluralization Rules]] provides more details how the `Lang::choice()` method works. The Blade `@choice` command uses the `Lang::choice()` command.
37 | {/discussion}
38 |
--------------------------------------------------------------------------------
/docs/recipes/config/get.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Retrieving a Specific Configuration Value
3 | Topics: configuration
4 | Code: Config::get()
5 | Id: 9
6 | Position: 3
7 | ---
8 |
9 | {problem}
10 | You want to access a configuration option.
11 | {/problem}
12 |
13 | {solution}
14 | Use `Config::get()`.
15 |
16 | To retrieve a specific configuration value.
17 |
18 | {php}
19 | $db_charset = Config::get('database.connections.mysql.charset');
20 | {/php}
21 |
22 | If the value doesn't exist, you can provide a default.
23 |
24 | {php}
25 | $default = Config::get('non.existant.config.key', 'I am default');
26 | {/php}
27 | {/solution}
28 |
29 | {discussion}
30 | Laravel caches configuration values within your request.
31 |
32 | The first time you retrieve a value within a configuration group, the entire group's configuration values are loaded and merged with any environment specific overrides (see [[Environment Specific Configurations]]). These values remain within the current request until it terminates.
33 |
34 | This means the next time you retrieve a configuration value for the group there's no need to load the value from the file system. The requested value is quickly returned.
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/cache/sear.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Getting an Item from the Cache or Storing a Default Forever
3 | Topics: cache
4 | Code: Cache::rememberForever(), Cache::sear()
5 | Id: 270
6 | Position: 19
7 | ---
8 |
9 | {problem}
10 | You want to retrieve an item from the cache.
11 |
12 | But, if it doesn't exist you want to store the value in the cache forever.
13 | {/problem}
14 |
15 | {solution}
16 | Use the `Cache::rememberForever()` value.
17 |
18 | {php}
19 | $value = Cache::rememberForever($key, function()
20 | {
21 | // fetch value from db or some other logic
22 | return $value;
23 | });
24 | {/php}
25 |
26 | If the item exists in the cache, it's returned immediately. But if it doesn't exist then the function is executed and the return value of the function is cached forever. In the later case this value is also returned.
27 | {/solution}
28 |
29 | {discussion}
30 | An alias to `Cache::rememberForever()` is `Cache::sear()`.
31 |
32 | This method is similar to the `Cache::remember()` method except you don't specify the `$minutes` because if the value is stored, it's stored permanently.
33 |
34 | See the [Retrieving an Item from the Cache or Storing a Default]] recipe.
35 | {/discussion}
36 |
--------------------------------------------------------------------------------
/docs/recipes/blade/append.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Stopping Injecting Content into a Section and Appending It
3 | Topics: -
4 | Code: @append
5 | Id: 243
6 | Position: 15
7 | ---
8 |
9 | {problem}
10 | You want to stop injecting content into a Blade section.
11 |
12 | And you want the content appended to any previous section of the same name.
13 | {/problem}
14 |
15 | {solution}
16 | Use the Blade `@append` command.
17 |
18 | {html}
19 | @section('test')
20 | one
21 | @stop
22 | @section('test')
23 | two
24 | @stop
25 | @yield('test')
26 | {/html}
27 |
28 | The above Blade template will output the following.
29 |
30 | {html}
31 | one
32 | {/html}
33 |
34 | But if you change the second `@stop` to an `@append`.
35 |
36 | {html}
37 | @section('test')
38 | one
39 | @stop
40 | @section('test')
41 | two
42 | @append
43 | @yield('test')
44 | {/html}
45 |
46 | Then the following is output.
47 |
48 | {html}
49 | one
50 | two
51 | {/html}
52 | {/solution}
53 |
54 | {discussion}
55 | You may want to use the `@parent` command.
56 |
57 | If you want to use the content of the previous section within your section, then see the [[Pulling in the Content of a Parent Section in Blade]] recipe.
58 | {/discussion}
59 |
--------------------------------------------------------------------------------
/docs/recipes/configuration/storage-perms.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Changing Storage Path Permissions
3 | Topics: Apache, configuration, Nginx, permissions
4 | Code: -
5 | Id: 43
6 | Position: 8
7 | ---
8 |
9 | {problem}
10 | You're receiving a Permission denied error.
11 |
12 | You believe this is because Laravel is trying to write something to the file system and doesn't have permissions to do so.
13 | {/problem}
14 |
15 | {solution}
16 | Change the owner or permissions of your storage path.
17 |
18 | {bash}
19 | $ cd app/storage
20 | $ sudo chmod 777 *
21 | $ sudo chmod 666 */*
22 | {/bash}
23 | {/solution}
24 |
25 | {discussion}
26 | It's likely a user issue.
27 |
28 | The instructions in the solution above make the storage directories _world writable_. The real issue is usually one thing.
29 |
30 | **Your console user and webserver user are different**
31 |
32 | This is normal, but often for development servers it's easier to make your webserver be the same user as your console user. The **Fixing Permissions** section of the two recipes listed below explain how to change this use for Apache and Nginx.
33 |
34 | * [[Creating an Apache VirtualHost]]
35 | * [[Creating a Nginx VirtualHost]]
36 | {/discussion}
37 |
--------------------------------------------------------------------------------
/docs/recipes/app/before.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Registering a Before Application Filter
3 | Topics: filters
4 | Code: App::before()
5 | Id: 53
6 | Position: 6
7 | ---
8 |
9 | {problem}
10 | You want to do work immediately before every request in your application.
11 | {/problem}
12 |
13 | {solution}
14 | Register a "before" application filter.
15 |
16 | {php}
17 | App::before(function($request)
18 | {
19 | if ($request->ajax())
20 | {
21 | // Returning a value will short-circut the life cycle and
22 | // keep any requests from being processed further
23 | return Response::json(['error' => 'AJAX not allowed']);
24 | }
25 | // No return value allows processing to continue as normal
26 | });
27 | {/php}
28 | {/solution}
29 |
30 | {discussion}
31 | You can movidy the request in application before filters.
32 |
33 | The `$request` object is an `Illuminate\Http\Request`.
34 |
35 | A common place to put "before" application filters is in the `app/filters.php` file.
36 |
37 | Be sure to understand exactly when application before filters are called. See the **Calls app before filters** in the Running Steps section of [[Understanding the Request Lifecycle]] for details.
38 | {/discussion}
39 |
--------------------------------------------------------------------------------