To define a route with a specific response status code in Laravel, you can use the response
helper function or the response
method on a controller to create the response. Here's an example:
Using the response
helper function in a web.php route file:
Route::get('/example', function () {
return response('Custom Response', 404);
});
Using the response
method in a controller:
namespace App\Http\Controllers;
use Illuminate\Http\Response;
class ExampleController extends Controller
{
public function example()
{
return response('Custom Response', 404);
}
}
In both cases, the response includes a string 'Custom Response' and a status code '404'. You can replace these with your desired content and status code.
Additionally, you can chain other methods on the response object to add headers, cookies, etc., as needed.