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
- ¿Cómo configurar PHP y Laravel desde cero?
- How do I set up a .gitlab-ci.yml file for a Laravel project using PHP?
- How can I use Xdebug to debug a Laravel application written in PHP?
- How do I configure Xdebug in the php.ini file for a Laravel project?
- How can I use XAMPP to develop a project in Laravel with PHP?
- How do I install Laravel using XAMPP and PHP?
- How can I set up a Telegram bot using PHP and Laravel?
- How can I troubleshoot a server error in a Laravel application built with PHP?
- How do I run a seeder in Laravel using PHP?
- How can I use the @yield directive in PHP Laravel?
See more codes...