Laravel is a robust PHP framework with a simple and elegant syntax that makes developing web applications quick and simple. Whether you are new to web development or an experienced developer looking to try Laravel, this step-by-step guide will walk you through the process of setting up a Laravel project from scratch.
Prerequisites
Before starting, ensure you have these installed on your computer:
- PHP (>= 7.3): Laravel requires PHP 7.3 or later.
- Composer: A dependency manager for PHP.
- Node.js and npm: Necessary for managing front-end assets.
- Git: Version control system to manage your project’s code.
- Text Editor or IDE: Visual Studio Code, PHP Storm, or any other text editor.
Step 1: Install Composer
Composer is essential for managing Laravel dependencies. You can download and install Composer from its official website: getcomposer.org.
After installation, verify Composer by running:
composer -v
A list of commands and the Composer version should be visible.
Step 2: You can create a Laravel project using Composer:
bash
Copy code
composer create-project –prefer-dist laravel/laravel project-name
Step 3: Configure Environment Settings
Navigate into your project directory:
cd project-name
Laravel uses a .env file for environment configuration. This file contains various settings such as database configuration, application URL, and more. Laravel provides a default .env file. Open it and configure your database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Check that your real database credentials are entered in the placeholders.
Step 4: Generate Application Key
Laravel uses an application key for security purposes. You need to generate this key by running:
php artisan key: generate
his sets the APP_KEY value in your .env file.
Step 5: Set Up Database
Before migrating the default database tables, ensure your database is running. Then, run the following command to migrate the default Laravel tables:
php artisan migrate
This command creates the necessary tables for user authentication and password resets.
Step 6: Serve the Application
You can now serve your Laravel application using the built-in PHP development server:
php artisan serve
This command starts a development server at http://localhost:8000. Open this URL in your web browser to see the default Laravel welcome page.
Step 7: Set Up Front-End Assets
Laravel integrates well with front-end build tools like Laravel Mix, which is built on top of Webpack. First, install the necessary Node.js dependencies:
npm install
Laravel Mix is configured in the webpack.mix.js file. To compile your assets (CSS and JavaScript), run:
npm run dev
For production builds, use:
npm run prod
Step 8: Set Up Version Control
Using version control for your projects is a good idea. Create a Git repository and push the first content:
git init
git add .
git commit -m “Initial commit”
Step 9: Customize Your Application
Now that your Laravel project is set up, you can start customizing it to fit your needs. Here are a few common tasks:
- Creating Routes: Define routes in the routes/web.php file.
Creating Controllers: Use Artisan to create controllers.
php artisan make: controller Your Controller Name
- Creating Views: Store your Blade templates in the resources/views directory.
- Working with Models: Create Eloquent models to interact with your database.
Conclusion
Congratulations! You’ve successfully set up a Laravel project from scratch. This guide covered the essential steps, from installing Composer to setting up front-end assets and version control. Laravel’s extensive documentation and community resources are always available to help you as you continue developing your application.
Happy coding!