Laravel basics: what the framework gives you
What Laravel is
Laravel is a PHP framework: a set of ready classes and a fixed project layout for building
web sites and JSON APIs. You write the code that is special to your project, and the framework
does the repeated work: it reads the request, finds the code for the URL, talks to the database
and sends the response.
It is open source, installed with Composer, and needs only PHP and a database to run.
If you know plain PHP, classes and
namespaces, you can start.
Main parts
Each part has its own lesson in the course.
- Routes: connect a URL and an HTTP method with the code that answers it. Lesson: Your first route
- Controllers: classes that hold the logic of the pages. Lesson: Controllers
- Blade: the template language for HTML pages. Lesson: Blade basics
- Eloquent: the ORM: one class per table, one object per row. Lesson: Eloquent models
- Migrations: database changes kept as PHP files in the project. Lesson: Migrations
- Validation: rules that check the input before your code uses it. Lesson: Validation
- Middleware: code that runs before or after a request, such as a login check. Lesson: Middleware
- Service container: creates objects and passes them their dependencies. Lesson: Service container
- Artisan: the command-line tool of the project. Lesson: Artisan commands
- Queues: move slow work, such as sending mail, out of the request. Lesson: Queues
How a request is handled
Every request enters through one file, public/index.php. Laravel starts, runs the middleware, and finds the route that matches the URL. The route calls a controller method. The controller reads or changes data through Eloquent models and returns a response: a Blade view for a browser or JSON for an API. The response goes back through the middleware to the visitor.
What a project looks like
The main folders of a new project:
laravel-app/ ├── app/ models, controllers, middleware ├── config/ settings ├── database/ migrations, seeders, factories ├── public/ index.php, the docroot ├── resources/ Blade views, CSS, JS ├── routes/ web.php and other route files ├── storage/ logs, cache, uploaded files ├── tests/ ├── .env settings of this machine └── artisan
When to choose Laravel
Laravel is a good choice for:
- pages, forms and a database behind them: a shop, a blog, a dashboard, an admin panel
- a JSON API for a mobile app or a JavaScript front end
- a project where you want one common way to do login, mail, queues and tests
For one static page or a small script a framework is more than you need: plain PHP is enough.
The other large PHP framework on this site is
Symfony.
Where to go next
Install the framework in lesson 2, Install Laravel, or see every lesson on the Laravel page.
Article author: Klavdiya Kondakova