php-laravelHow do I create a PHP Laravel API example?
Creating a PHP Laravel API example is fairly straightforward. First, create a Laravel project using the laravel new command:
laravel new my-project
This will create a new project in the my-project directory.
Next, create a controller in the app/Http/Controllers directory. This controller will handle the API requests. For example:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function index()
{
return response()->json([
'message' => 'Hello World!'
]);
}
}
This controller will return a JSON response with the message "Hello World!".
Finally, create a route in the routes/web.php file to map the URL to the controller:
Route::get('/api', 'ApiController@index');
Now when you visit the /api URL, you will get the JSON response.
Helpful links
More of Php Laravel
- How do I decide between using PHP Laravel and Yii for my software development project?
- How can I use React with PHP Laravel?
- How can I create a website using the Laravel PHP framework and a template?
- How can I use PHP Laravel to create a Wikipedia page?
- How can I use Laravel and JavaScript together in a PHP application?
- How can I find PHP Laravel jobs?
- How can I convert JSON data to XML using PHP Laravel?
- How do I set up notifications in a Laravel application using PHP?
- How do I generate an app_key for my Laravel PHP application?
- How do I deploy a Laravel application to a Kubernetes cluster using PHP?
See more codes...