Lesson 1
Very First Laravel App
Very First Laravel App

Welcome aboard your journey into building your first Laravel application! This is your introduction to Laravel, a powerful PHP framework known for its elegant syntax and robust feature set. In this lesson, we will take our first steps by exploring the Laravel project structure. Whether you're completely new to Laravel or need a gentle refresher, understanding this foundational aspect is crucial before moving forward.

What You'll Learn

In this section, you will learn how to create a simple Laravel app and understand its fundamental project structure. We'll explore what views are and how to integrate HTML using Blade, Laravel's templating engine. This knowledge will provide a solid foundation, enabling you to build engaging web pages quickly.

Here’s how a basic Laravel route and view are set up:

app/routes/web.php

php
1<?php 2 3use Illuminate\Support\Facades\Route; 4 5Route::get('/', function () { 6 return view('welcome'); 7});

app/resources/views/welcome.blade.php

HTML, XML
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Hello World</title> 7</head> 8<body> 9 <h1>Cosmo is Welcoming You!</h1> 10</body> 11</html>

Let's break down what the above code does:

  • app/routes/web.php defines a route that returns a view named welcome when the user visits the root URL /.
    • If you visit http://localhost:8000/, you'll see the content of the welcome.blade.php view. The view is stored in the resources/views directory, and the .blade.php extension indicates that it uses Blade templating - which is a powerful feature of Laravel that simplifies working with HTML.
    • The view() function is a helper method that returns the content of the specified view file. In this case, it returns the content of welcome.blade.php.
  • app/resources/views/welcome.blade.php contains the HTML content that will be displayed when the user visits the root URL. The Blade syntax allows you to write PHP code within the HTML file, making it easier to work with dynamic content. We'll explore Blade templating in more detail later.

With this basic setup, you can create a simple web page using Laravel. As you progress through this lesson, you'll learn more about Laravel's features and how to build more complex applications.

Project Structure

Laravel follows the Model-View-Controller (MVC) architectural pattern, which separates the application into three main components:

  • Models: Represent the data structure of the application.
  • Views: Display the user interface.
  • Controllers: Handle user requests and interact with the models.

The Laravel project structure is designed to organize your codebase effectively. Here's an overview of the key directories and files in a Laravel project:

Plain text
1app 2├── app 3│ ├── Console // Contains console commands 4│ ├── Exceptions // Contains exception handling classes 5│ ├── Http 6│ ├── Models 7│ └── Providers 8├── config 9├── database 10│ ├── factories 11│ ├── migrations 12│ └── seeders 13├── public 14├── resources // Contains assets like views, CSS, and JavaScript files 15│ ├── css 16│ ├── js 17│ └── views 18├── routes 19├── storage // Used for storing cache, sessions, and logs 20│ ├── app 21│ ├── framework 22│ └── logs 23├── tests // Used for testing 24│ ├── Feature 25│ └── Unit
  • app: Contains the core application code, including models, controllers, and service providers. Note, that this is note the same as the app directory in the root of the project which contains the application's configuration files. The app directory we're referring to here is located inside the project root directory: app/app in our case.

    • app/Http: Contains controllers, middleware, and requests used to handle HTTP requests.
    • app/Models: Contains the application's data models that interact with the database.
    • app/Providers: Contains service providers that bootstrap the application and bind services to the container.
  • config: Contains configuration files for the application, such as database settings, cache configurations, and more.

    • config/app.php: Contains the application configuration settings.
    • config/database.php: Contains the database connection settings.
  • database: Contains database migrations, seeders, and factories.

  • public: Contains the front controller (index.php) and assets like images, CSS, and JavaScript files. The index.php file serves as the entry point to your Laravel application. When a request is made to the application, it is directed to this file, which then boots the Laravel framework and handles the request. Unlike traditional PHP applications where you might have multiple PHP files handling different URLs, Laravel consolidates everything behind a single entry point, improving security and scalability.

  • routes: Contains route definitions for the application.

    • routes/web.php: Contains routes that respond to HTTP requests.

There are many other directories and files in a Laravel project, but these are some of the key ones you'll encounter frequently. As you work with Laravel, you'll become more familiar with the project structure and how to navigate it effectively.

We'll explore these directories and files in more detail as we progress through the course path. Understanding the project structure is essential for building robust applications and maintaining them effectively.

Why It Matters

Understanding the project structure is vital as it influences how you organize code, manage resources, and maintain your application over time. Laravel’s structure promotes best practices and enhances productivity, making your development process smoother and more enjoyable. With a grasp of these basics, you'll be equipped to dive deeper into more advanced features of Laravel with confidence.

Exciting, isn’t it? Let’s jump into the practice section and bring these concepts to life together.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.