INT 221 - MVC
PROGRAMMING
File and Folder Structure
Directory/Application Structure
• Important File and Folder
• Where we create MVC Files
• Basics of all folders
Root Directory (Project Name)
When we build Project:
laravel new project_name
Directory Structure
composer.json- Defines Composer
dependencies
composer.json file-Project Details
and dependencies
• Used to manage the project’s PHP dependencies. It is
a JSON file that defines the metadata, dependencies,
and scripts for the project.
• When you run Composer commands, like composer
install or composer update, the composer.json file
guides Composer in fetching and managing
dependencies.
composer.json file-Beaware of
deleting
• If deleted, and file can’t be recovered then it is impossible
to start the Laravel project
• Contains Details of the project
Eg. Version of PHP, Version of Laravel
• Contains dependencies, libraries
Key Sections of composer.json in
Laravel
name: Specifies the package name in the format vendor/package.
"name": "laravel/laravel"
description: A short description of
the project.
"description": "The Laravel Framework."
require: Lists the dependencies required for
the project to work. Laravel uses specific
versions of libraries.
"require": {
"php": "^8.0",
"laravel/framework": "^10.0",
"guzzlehttp/guzzle": "^7.0"
}
require-dev: Specifies
development dependencies like
testing tools.
"require-dev": {
"fakerphp/faker": "^1.9.1",
"phpunit/phpunit": "^9.5"
}
autoload: Configures the autoloading for PHP
classes. Laravel primarily uses the psr-4
autoloading standard.
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
autoload-dev: Defines autoloading
for development-only files, often
used for tests.
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
Vendor
• All the dependencies mentioned in the Composer.json
file are stored in vendor folder
Vendor folder (mentioned in
composer.json and stored in
Vendor)
• The vendor folder in a Laravel project is where
Composer stores all the third-party dependencies
required for the application.
• It is automatically generated and managed by
Composer and includes libraries, frameworks, and
tools specified in the composer.json file.
Autoloading:
• The vendor/autoload.php file is the main entry point
for Composer's autoloading mechanism.
• Laravel's bootstrap/app.php includes this file to
autoload all necessary classes
Organizational Structure:
• Dependencies are organized in subdirectories based
on their namespaces (e.g., vendor/symfony/,
vendor/laravel/).
• It also contains the composer/ directory, which
manages autoloading and metadata about the
dependencies.
App Folder
• Contains the core code of the project
• The app folder in Laravel is a key directory where most of
your application logic resides.
• It is where you define the core of your application, including
its business logic, controllers, models, service provider and
other components.
App Folder in Laravel
Providers
• Stores service provider classes.
• Service providers are responsible for bootstrapping your application.
• By default, Laravel includes several providers, such as
AppServiceProvider, AuthServiceProvider, and EventServiceProvider.
routes/web.php (opened at which
link)
• In Laravel, the routes/web.php file is where you
define routes for web interfaces of your application.
• These routes typically respond to requests made via a
web browser and are grouped within the web
middleware group, which provides features such as
session handling, CSRF protection, and cookie
encryption.
Structure of web.php
Route::get('/uri', function () {
return 'Response';
});
Common HTTP Methods:
• GET: For retrieving data (e.g., showing a form or page).
• POST: For submitting data (e.g., processing forms).
• PUT/PATCH: For updating data.
• DELETE: For deleting data
Basic Route Example:
Route::get('/', function () {
return view('welcome');
});
routes/console.php file (Console
commands)
• The routes/console.php file in Laravel is where you
define all custom Artisan commands for your
application.
• This file allows you to extend and customize the
functionality of Laravel's command-line interface
(CLI)/terminals.
• Not used directly
Resources Folder (Contains
Views)
• In Laravel, views are used to separate the presentation layer
from the business logic of your application.
• Views are typically HTML templates that are located in the
resources/views directory.
• They allow you to display data passed from controllers or
routes.
Create a view file in the
resources/views directory, e.g.,
welcome.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h1>Welcome to Laravel</h1>
</body>
</html>
Use the view() helper function to render it:
Route::get('/', function () {
return view('welcome');
});
Passing Data to Views
Route::get('/greet', function () {
return view('greet', ['name' => 'John']);
});
In the view (greet.blade.php):
<h1>Hello, {{ $name }}</h1>
Alternative Syntax:
• return view('greet')->with('name', 'John');
Passing multiple variables:
return view('greet', ['name' => 'John', 'age' => 25]);
config folder
• The config folder in Laravel contains configuration files
(Configuration of cache, application, authorization,
login, session ) for various aspects of your application.
• These files allow you to define settings and options
that your application can use.
• Each configuration file corresponds to a specific
feature or service, such as the database, caching, or
mail services.
Database Folder
• Connection with Database (Connection File) is to be made in
Config database.php
In a Laravel project, the config/database.php file contains the database
connection settings for the application. This file defines the database
connections, including the default connection, credentials, and options for
various database drivers like MySQL, PostgreSQL, SQLite, and SQL Server.
the database folder
• In Laravel, the database folder is a directory where database-
related resources, such as migrations, model factories,
seeders, and testing data, are stored.
• It is part of Laravel’s scaffolding that helps in database
management and testing.
The public folder
• The public folder in Laravel serves as the entry point for
your application. First file that get executed in index.php
• configures autoloading
• It is the directory where all public-facing files reside, such as
assets, images, Javascipt, CSS and favicon icon and the
primary PHP file that handles incoming requests.
The storage folder
• The storage folder in Laravel is used to store files generated or
used by the application (pdf, xlsx).
• It contains subdirectories for logs (fetch the errors), cached
data, file uploads, and other types of files.
• This folder plays a vital role in Laravel’s functionality, such as
caching, file storage, and logging.
Tests folder- tests/ (Automated
Testing)
• Contains PHPUnit test cases.
• Unit Testing
Composer.lock
• The composer.lock file in Laravel (and any PHP project using
Composer) is an important file that ensures consistency in
your project’s dependencies.
• It works alongside the composer.json file to manage package
versions and their relationships.
• Views files are present in resources folder
• Models are present in app folder(User.php)
• Controllers are present in app Http/Controllers
Summary of Laravel File &
Folder Structure
Video
• https://www.youtube.com/watch?
v=L7QxTLhsuVk&ab_channel=CodeStepByStep