Laravel 5.4
Awesome Laravel
- Awesome Laravel (Chirag Gude)
Prologue
- Release Notes
- Upgrade Guide
Getting Started
- Installation
- Configuration
- Directory Structure
- Laravel Homestead
- valet
Architecture Concepts
- Request Lifecycle
- Service Container
- Service Providers
- Facades
The Basics
- Routing
- Errors & Logging
- Middleware
- CSRF Protection
- Controllers
- HTTP Requests
- HTTP Responses
- Views
- HTTP Session
- Validation
Frontend
- Blade Templates
- Localization
- JavaScript & CSS Scaffolding
- Compiling Assets (Laravel Mix)
Security
- Authentication
- API Authentication (Passport)
- Authorization
- Encryption
- Hashing
- Resetting Passwords
Digging Deeper
- Artisan Console
- Queues
- Package Development
- Task Scheduling
- Broadcasting
- Cache
- Collections
- Events
- File Storage
- helpers
- Notifications
Database
- Database Getting Started
- Database Query Builder
- Database Pagination
- Database Migrations
- Database Seeding
- Redis
Eloquent ORM
- Eloquent Getting Started
- Eloquent Relationships
- Eloquent Collections
- Eloquent Mutators
- Eloquent Serialization
Testing
- Testing Getting Started
- HTTP Tests
- Browser Tests (Laravel Dusk)
- Database Testing
- Mocking
- redirect
Official Packages
- Laravel Cashier
- Envoy Task Runner
- Laravel Scout
Views
Creating Views
{tip} Looking for more information on how to write Blade templates? Check out the full Blade documentation to get started.
Views contain the HTML served by your application and separate your controller / application logic from your presentation logic. Views are stored in the resources/views
directory. A simple view might look something like this:
Since this view is stored at resources/views/greeting.blade.php
, we may return it using the global view
helper like so:
As you can see, the first argument passed to the view
helper corresponds to the name of the view file in the resources/views
directory. The second argument is an array of data that should be made available to the view. In this case, we are passing the name
variable, which is displayed in the view using Blade syntax.
Of course, views may also be nested within sub-directories of the resources/views
directory. “Dot” notation may be used to reference nested views. For example, if your view is stored at resources/views/admin/profile.blade.php
, you may reference it like so:
Determining If A View Exists
If you need to determine if a view exists, you may use the View
facade. The exists
method will return true
if the view exists:
Passing Data To Views
As you saw in the previous examples, you may pass an array of data to views:
When passing information in this manner, the data should be an array with key / value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>
. As an alternative to passing a complete array of data to the view
helper function, you may use the with
method to add individual pieces of data to the view:
Sharing Data With All Views
Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade’s share
method. Typically, you should place calls to share
within a service provider’s boot
method. You are free to add them to the AppServiceProvider
or generate a separate service provider to house them:
View Composers
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
For this example, let’s register the view composers within a service provider. We’ll use the View
facade to access the underlying Illuminate\Contracts\View\Factory
contract implementation. Remember, Laravel does not include a default directory for view composers. You are free to organize them however you wish. For example, you could create an app/Http/ViewComposers
directory:
{note} Remember, if you create a new service provider to contain your view composer registrations, you will need to add the service provider to the
providers
array in theconfig/app.php
configuration file.
Now that we have registered the composer, the ProfileComposer@compose
method will be executed each time the profile
view is being rendered. So, let’s define the composer class:
Just before the view is rendered, the composer’s compose
method is called with the Illuminate\View\View
instance. You may use the with
method to bind data to the view.
{tip} All view composers are resolved via the service container, so you may type-hint any dependencies you need within a composer’s constructor.
Attaching A Composer To Multiple Views
You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer
method:
The composer
method also accepts the *
character as a wildcard, allowing you to attach a composer to all views:
View Creators
View creators are very similar to view composers; however, they are executed immediately after the view is instantiated instead of waiting until the view is about to render. To register a view creator, use the creator
method: