Here’s a short tutorial to help you get started with Laravel:
- Install Laravel: To install Laravel, you’ll need to have PHP and Composer installed on your system. You can then install Laravel using Composer by running the following command in your terminal:luaCopy code
composer create-project --prefer-dist laravel/laravel my-project
This will create a new Laravel project in a directory called “my-project”. - Configure Your Database: Laravel comes with built-in support for a variety of databases, including MySQL, PostgreSQL, and SQLite. To configure your database, edit the .env file in your project directory to add your database credentials.
- Create a Route: In Laravel, routes are defined in the routes/web.php file. To create a new route, simply add a new entry to the file. For example, to create a route that displays a welcome message, you can add the following code:javascriptCopy code
Route::get('/', function () { return 'Welcome to my Laravel app!'; });
- Create a Controller: Controllers are used to handle requests and return responses. To create a new controller, you can use the “make:controller” Artisan command. For example, to create a new controller called “HomeController”, run the following command in your terminal:goCopy code
php artisan make:controller HomeController
- Add a Method to the Controller: Once you’ve created your controller, you can add a method to handle your route. For example, to modify the route we created earlier to use the “index” method of the HomeController, you can modify the code in the routes/web.php file to look like this:cssCopy code
Route::get('/', 'HomeController@index');
Then, in the HomeController.php file, add the following code to the index method:csharpCopy codepublic function index() { return view('welcome'); }
- Create a View: Views are used to display the HTML content of your application. To create a new view, you can use the “make:view” Artisan command. For example, to create a new view called “welcome.blade.php”, run the following command in your terminal:goCopy code
php artisan make:view welcome
Then, in the resources/views/welcome.blade.php file, add the following code:phpCopy code<!DOCTYPE html> <html> <head> <title>Welcome to my Laravel app!</title> </head> <body> <h1>Welcome to my Laravel app!</h1> </body> </html>
- Run Your Application: Finally, you can run your Laravel application by starting the built-in web server. To do this, run the following command in your terminal:Copy code
php artisan serve
This will start the web server on http://localhost:8000. Open this URL in your web browser to see your welcome message.
By following this tutorial, you should now have a basic understanding of how to create routes, controllers, views, and databases in Laravel, and how to start your application. You can continue to explore the many features and capabilities of Laravel to create more advanced web applications.