Creating a Contributor Maintenance Policy for Laravel Packages
When you maintain a Laravel package, contributors need to know how to test their changes and update user documentation. They also need…
When a Laravel web route needs to display information from its URL, the route or its controller must pass that information to a Blade template. A Blade template is a .blade.php file that Laravel uses to build a page, such as a page for a single post. A route parameter like {post} arrives with the request, and Laravel does not automatically turn it into a variable in the template.
In most cases, you receive the route parameter, look up or prepare the data the page needs, and return a view with an associative array that pairs each key with a value. Laravel then makes each key available as a variable in the template. The examples below follow Laravel 12 conventions.
When a page needs a database record or extra logic to prepare its data, let a controller handle that work and return the view. Laravel maps the view name posts.show to resources/views/posts/show.blade.php.
// routes/web.php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::get('/posts/{post}', [PostController::class, 'show']);
When you type-hint the controller argument as Post, Laravel uses implicit route model binding to look up the database record for {post} and pass it in as a Post model:
// app/Http/Controllers/PostController.php
use App\Models\Post;
public function show(Post $post)
{
return view('posts.show', ['post' => $post]);
}
The controller passes the model under the key post, so the template receives it as $post:
{{-- resources/views/posts/show.blade.php --}}
<h1>{{ $post->title }}</h1>
{{ $post->body }}
When Laravel cannot find the requested model during implicit binding, it returns a 404 response. Laravel finds the record, but it does not check whether the current user is allowed to view it. You still need to check authorization separately when the page shows protected data.
If a route receives a plain value instead of a bound model, accept the parameter in your route callback or controller and pass it to the view yourself:
Route::get('/greetings/{name}', function (string $name) {
return view('greetings', ['name' => $name]);
});
When a route declares multiple URI parameters, Laravel supplies the matching callback or controller arguments in the order they appear in the route. Add ? to an optional route parameter and give its method argument a default value, such as ?string $name = null. See Laravel’s routing documentation for details about parameters and binding.
Route::view for a simple pageIf a route returns a view with fixed data, use Route::view instead of writing a closure or controller:
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
You pass the view's data as the third argument, which makes $name available inside resources/views/welcome.blade.php. Use Route::view when the page only needs static values. If you need to query a database, branch on request data, or run other logic, return the view from a controller instead. Keep in mind that Laravel reserves view, data, status, and headers as parameter names for view routes. You can read more about direct view responses and passing data in the view documentation.
When you pass an array as the second argument to view(), anyone reading the controller can immediately see which values the template receives:
return view('posts.show', [
'post' => $post,
'relatedPosts' => $relatedPosts,
]);
You can also chain with() if you prefer to add values one at a time:
return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');
Pick clear key names that match what the template expects. Let your controller or application code handle database queries and decisions, and leave Blade to display the prepared values.
When you want to attach data every time Laravel renders a specific view, regardless of which controller returns it, you can register a view composer. If every view in your app needs the same value, you can share it with View::share. Use both features sparingly so your templates do not rely on hidden data that is hard to trace. The Laravel view documentation covers view composers and shared data in detail.
By default, Blade’s {{ $value }} syntax converts characters that have special meaning in HTML into safe text. Use it for ordinary text, especially any value that comes from a user or a database:
<h1>{{ $post->title }}</h1>
By contrast, {!! $value !!} outputs raw content without escaping it. Use it only for trusted content that you have sanitized according to your policy. If you render user-supplied HTML this way, you can expose visitors to cross-site scripting.
Blade's HTML escaping does not check whether a URL scheme is safe, and it does not encode values for JavaScript or CSS. When you need to pass a PHP variable into JavaScript, Laravel provides Js::from($value) to safely render it as JSON. Match your encoding to each output context by following the OWASP Laravel cheat sheet and Laravel’s Blade documentation.
A route parameter comes from an incoming URL, whereas view data is what your route or controller passes to a template. When you call route('profile', ['id' => 1]), Laravel generates a URL for a named route and does not pass $id to the current view. To send data to the template, call view('profile', ['id' => 1]) instead. Laravel’s routing documentation explains how URL generation works for named routes.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com