I am working on a Laravel application and I come across a very strange issue. I have defined the routes using the Route::resource function
Route::resource('posts', 'PostController');
and I generate the PostController using
php artisan make:controller PostController --resource --model=Post
command so it is using Implicit Binding for Post resource.
PostController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
//
}
}
Now the URLs are
GET /posts
GET /posts/create
POST /posts
GET /posts/{post}
GET /posts/{post}/edit
PUT/PATCH /posts/{post}
DELETE /posts/{post}
Everything is working fine. Let say I have a post whose id is 146. so the URL for the post detail page will be
http://localhost/laravel-app/posts/146
This URL is working fine. But when I open the following URL it still working.
http://localhost/laravel-app/posts/146re4dasdasda
http://localhost/laravel-app/posts/146re4
http://localhost/laravel-app/posts/146re
So I expected that the above mention URLs should return a 404 not found page. But it still shows the detail page for the post whose id is 146.
and the URLs are also working for edit, update or delete operations. i.e
GET /posts/146re4/edit
PUT/PATCH /posts/146re4
DELETE /posts/146re4
My Answer:
Implicit binding in Laravel allows you to automatically inject model instances into your routes based on the route parameter name. This means that when you define a resource route like `Route::resource('posts', 'PostController');`, Laravel will automatically bind the `Post` model to the `id` parameter in the route.
So, even if the URL consists of alphanumeric characters for the resource id, Laravel will still be able to retrieve the correct `Post` model instance using Implicit Binding. This is because Laravel will try to find a `Post` model with the specified alphanumeric id in the database and inject it into the controller method.
In summary, Implicit Binding in Laravel allows you to work with resource routes using alphanumeric ids without any issues.
Rate this post
5 of 5 based on 9820 votesComments