Crafting a new laravel app
Here's a step-by-step guide to crafting a new Laravel app:
Step 1: Install Laravel
Open your terminal and run the following command to install Laravel:
composer create-project --prefer-dist laravel/laravel project-name
Replace project-name
with the desired name of your project.
Step 2: Set up the project structure
Laravel comes with a default project structure. You can explore the directory structure by running the following command:
cd project-name
ls
You'll see the following directories and files:
app
: This is where you'll write your application code.bootstrap
: This directory contains files that are executed during the bootstrapping process.config
: This directory contains configuration files for your application.database
: This directory contains database migration files and seeds.public
: This directory contains publicly accessible files, such as images and CSS files.resources
: This directory contains files that are used by your application, such as views and translations.routes
: This directory contains route files that define how your application responds to HTTP requests.storage
: This directory contains files that are used by your application, such as logs and cache files.tests
: This directory contains test files for your application.vendor
: This directory contains third-party libraries and dependencies.composer.json
: This file contains dependencies and metadata for your project.composer.lock
: This file contains the exact versions of dependencies that are installed.
Step 3: Set up the database
Laravel uses a database to store data. You'll need to set up a database and configure Laravel to use it. Here are the steps:
-
Create a new database: Run the following command to create a new database:
mysql -uroot -p<password> -e "CREATE DATABASE project_name"
Replace
<password>
with your MySQL password andproject_name
with the desired name of your database. -
Configure Laravel to use the database: Open the
config/database.php
file and update thedefault
database connection to use your new database:'default' => env('DB_CONNECTION', 'mysql'), 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'project_name'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],
Replace
project_name
with the name of your database.
Step 4: Set up the environment
Laravel uses environment variables to configure the application. You'll need to set up the environment variables for your application. Here are the steps:
- Create a new
.env
file: Run the following command to create a new.env
file:cp.env.example.env
- Update the environment variables: Open the
.env
file and update the environment variables to match your application's requirements.
Step 5: Run the migrations
Laravel uses migrations to manage database schema changes. You'll need to run the migrations to create the necessary tables in your database. Here are the steps:
- Run the migrations: Run the following command to run the migrations:
php artisan migrate
This will create the necessary tables in your database.
Step 6: Create a new controller
Laravel uses controllers to handle HTTP requests. You'll need to create a new controller to handle requests for your application. Here are the steps:
- Create a new controller: Run the following command to create a new controller:
php artisan make:controller MyController
This will create a new
MyController.php
file in theapp/Http/Controllers
directory.
Step 7: Define routes
Laravel uses routes to define how your application responds to HTTP requests. You'll need to define routes for your application. Here are the steps:
- Define routes: Open the
routes/web.php
file and define routes for your application. For example:Route::get('/', function () { return 'Hello, World!'; });
This will define a route that responds to GET requests to the root URL (
/
) with the message "Hello, World!".
Step 8: Run the application
You're now ready to run your application! Run the following command to start the development server:
php artisan serve
This will start the development server and you can access your application by visiting http://localhost:8000
in your web browser.
That's it! You've now set up a new Laravel app and are ready to start building your application.