Getting started with Laravel- PHP Laravel Framework for Beginners

by jay patel




Laravel Framework is a popular PHP framework that is taking the web development community by storm. Laravel is the Most Starred PHP Framework on Github: more than 30 000 developers from all over the world (mostly from the USA) use it for custom software development. 

Installing Laravel Homestead Virtual Machine

#1 INSTALLING LARAVEL HOMESTEAD VIRTUAL MACHINE

Use Laravel Homestead (pre-packaged Vagrant box) as your development environment. Vagrant is the command line utility for building and maintaining portable virtual development environments.

Laravel Homestead runs on any Windows, Mac, or Linux system, and includes the Nginx web server, PHP 7.0, MySQL, Postgres, Redis, Memcached and a variety of other useful utilities. With Laravel Homestead it is not required to separately install PHP, a web server, and any other server software on your local machine.

 

#2 INSTALLING LARAVEL FROM COMPOSER

Laravel framework consists of many separate components that are downloaded and put together automatically into a framework through a special tool called “Composer” (a tool for dependency management in PHP). So, before using Laravel, make sure you have Composer installed on your machine.

 

#3 USING ARTISAN COMMAND-LINE INTERFACE IN LARAVEL

Artisan is the command-line interface included with Laravel.  Artisan offers many useful commands that can help you perform various tasks, such as generating migrations or publishing a package's assets. In addition to the built-in commands, you can extend Artisan with your own commands.

Artisan comes with Laravel by default. If your php command works fine, then the only thing you need to do is to navigate to the project's root folder. The root folder is the parent folder of the app folder.

 

#4 CREATING ROUTES AND LOADING VIEWS WITHOUT CONTROLLERS IN LARAVEL

The "hello world" for frameworks is to figure out how to process a given URI, and display the appropriate HTML on the page.

Routes define the endpoints of a web application, the final destinations of incoming requests. All Laravel routes are defined in the route files, which are located in the routes directory. These files are automatically loaded by the framework. For most applications, you will begin by defining routes in your routes/web.php file.

Views are used to build up the resulting page that the user will see in their browser when they click on a link or submit a form. Typically a view is the HTML response returned by the application to the browser initiating the request.  Views can be loaded directly from the routing file.  Views are stored in the resources/views directory. View files have a .php file extension or a .blade.php file extension if you use the Blade templating engine.

Using route closures (defining all of your request handling logic as closures in route files, without controllers) is very useful for small projects that don't require too much functionality. Closure based routes cannot be cached. To use route caching, you must convert any closure routes to controller classes.

 

#5 CREATING ROUTES AND LOADING VIEWS WITH CONTROLLERS IN LARAVEL

For most projects you may wish to organize request handling logic using controllers. Controllers are PHP classes that contain application’s logic and are called from the application’s routing files. Controllers contain methods called “actions” that direct the execution flow of a web application by working with application’s data (models), producing output (views) or redirecting. Controllers can group related request handling logic into a single class. The controllers need to be connected to the routing mechanisms in order to be executed when the application runs. Controllers are stored in the app/Http/Controllers directory.

By creating a controller and specifying which routes in the routing file use its member functions you are able to take full advantage of MVC pattern in your Laravel applications.

Another important advantage of using controllers with routes instead of putting all code into the “route.php” file is that using separate PHP classes to build application’s functionality brings the full benefits of Object Oriented Programming (OOP) such as dependency injection, encapsulation, inheritance and more. This aspect of application architecture is incredibly important for developers who practice good design patterns in their applications.

If your application is using controller based routes, you should take advantage of Laravel's route cache. Using the route cache will drastically decrease the amount of time it takes to register all of your application's routes. In some cases, your route registration may even be up to 100x faster.

 

#6 USING BLADE TEMPLATING LANGUAGE TO CREATE TEMPLATES IN LARAVEL

Laravel uses its own view template engine called “Blade”. Blade template engine is fantastic for reducing or simplifying the code that you write in your view. Blade template engine also allows you to easily create layouts, extend them and include partials to prevent you from repeating the same HTML in multiple files. Using Blade makes it is possible to combine many templates (for example, header.blade.php, footer.blade.php and sidebar.blade.php) into a single HTML output. To reduce duplication, Blade provides "layout files." Within these files, you can define the wrapping HTML, which nests the content from each of your views.

adding css js to laravel

 

#7  ADDING/MANAGING YOUR CSS AND JS FILES IN LARAVEL

When it comes to including your assets (CSS and JavaScript files), you can create directories within your public folder and add it directly in your view file. This is great for small projects.

laravel blade template engine

But if you need to use CSS preprocessors, or concatenate some files and minify them, Laravel Elixir (a tool to integrate Gulp into your Laravel projects, a task runner built as a wrapper around Gulp) will help you.

Gulp is a well-known JavaScript-based task runner - an automated tool for performing repetitive tasks like concatenation, minification, unit testing, linting etc. With Gulp you can create tasks to watch, compile, concatenate, minify and auto-prefix your CSS and Sass/Less, concatenate and minify JS, minify HTML and optimise images.

Elixir is built on top of Gulp, so to run your Laravel Elixir tasks you only need to run the gulp command in your terminal. Make sure that you install Gulp globally first.

 

#8 DATABASE MANAGEMENT AND DATABASE OPERATIONS IN LARAVEL

Currently, Laravel supports four database systems: MySQL, Postgres, SQLite, SQL Server. The database configuration for your application is located at config/database.php. In this file you may define all of your database connections, as well as specify which connection should be used by default.

Laravel interprets the data stored in the database into objects that you can easily manipulate like you would manipulate any other PHP object. This allows the developer to focus on other parts of the application instead of figuring out complex Structured Query Language (SQL) queries necessary to work with the data.

Laravel comes with two powerful sets of features to execute database operations, by using its own Query Builder or by using concept of “models” in Eloquent ORM. You can use both in the same application (even combine both to get the most flexibility out of DB operations).

Laravel’s Query Builder provides a clean and simple interface for executing database queries. It is a powerful tool that can be used for various database operations such as retrieving records, inserting new records, deleting records, updating records, performing “Join” queries, executing raw SQL queries, filtering, grouping and sorting of records

Laravel makes interacting with databases extremely simple usingthe Eloquent ORM (the most preferred way to fetch data and render it in the view). The Eloquent ORM provides an ActiveRecord implementation to work with your database. This means that each model corresponds to a table in your database. We can use Eloquent to interact with our database and do the basic CRUD functions, set up and use one-to-one relationships, one-to-many relationships, AND many-to-many relationships.

Some of the advanced database operations that Laravel provides out of the box are managing database tables and fields through special files called “migrations”, populating database with sample data (also called seeding”), working with separate read/write databases, pagination, many different relationship types and advanced methods of querying relations, database transactions and query logging.

 

#8.1 LARAVEL CRUD OPERATIONS

Forms exist in just about every single web application. How does it work in Laravel? Let's review the basic workflow for submitting a form, fetching the data that was sent through, and then creating a record in the database.

You'll of course need to modify existing records in the database. Let's review the process of creating an "edit" page to handle this very task. Along the way, we'll also get a chance to review PATCH requests and the concept of eager loading and the problems it solves.

Along the way, you'll be introduced to custom packages, service providers, and much more.

 

#8.2 VALIDATION IN LARAVEL

Laravel provides several different approaches to validate your application's incoming data. Let's look at a complete example of validating a form and displaying the error messages back to the user.

 

#8.3 AUTHENTICATION AND AUTHORISATION IN LARAVEL

Most applications will require some form of authentication. With Laravel, it is simple. Let's setup registration, authentication, and password resets in minutes.

 

#9 USING LARAVEL MIDDLEWARE

Middleware provide a convenient mechanism for filtering HTTP requests entering your application. There are many more cases where you would like to use a middleware: changing the site language based on locale, enabling site-wide maintenance, sniffing bot traffic, logging etc. There are several middleware included in the Laravel framework, including middleware for authentication and CSRF protection. All of these middleware are located in the app/Http/Middleware directory. Additional middleware can be written.

 

#10 LARAVEL FLASH MESSAGES EXAMPLE

Often, you'll want to send a quick notification to the user when they perform some kind of action in your application. "Good job, your task has been created." Or: "You are now logged out." So it seems that we need a way to store things in the session for just a single request. There are some clean ways to handle this in Laravel!

 

#11 LARAVEL SERVICE CONTAINER

Laravel's service container (also known as the IoC container) is the heart of Laravel. It is a powerful tool for managing class dependencies and performing dependency injection.

Basically the IoC Container is just an ordinary PHP class. But we can place or “Bind” in it everything we need from interfaces implementations to directories paths and so on. Now, since we have a single Object that contains all of our various bindings, it is very easy to retrieve them back or “resolve” them at any point in our code.

 

#12 LARAVEL SERVICE PROVIDERS

Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers.
But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application. For example, when installing a package, you have to give application access to that package - one of the best solutions is through Service Providers list and a Facade.

Laravel ships with many facades which provide access to almost all of Laravel's features. In a Laravel application, a facade is a class that provides access to an object from the container.

Facades have many benefits. They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. Furthermore, because of their unique usage of PHP's dynamic methods, they are easy to test. However, some care must be taken when using facades.

 

LARAVEL STARTER KITS

Videos are obviously useful without a doubt. However, many will agree that nothing compares to a working example of a functional code.

 

LARAVEL COMMUNITY

Laravel Community are free discussion forums for asking and answering questions about using Laravel framework.

 

TWITTER ACCOUNTS TO FOLLOW

Taylor Otwell, Dayle Rees, Shawn McCool, Jeffrey Way, Chris Fidao, Phil Sturgeon, Jens Segers, Ben Corlett, Matt Stauffer, Graham Campbell Belitsoft, Advanceideainfotech

 

DO YOU NEED TO READ SOME PDF BOOKS ABOUT LARAVEL?

Ref: Belitsoft



Leave a Reply

Your email address will not be published. Required fields are marked *

   Confirm you are not a spammer
   Notify me of follow-up comments by email.