قالب وردپرس درنا توس
Home / Tips and Tricks / What is Laravel and how do you get started? – CloudSavvy IT

What is Laravel and how do you get started? – CloudSavvy IT



Laravel logo

Laravel is a web framework for creating custom applications. It runs on PHP and is completely free and open source. We discuss what makes this framework a good choice and why you might want to base your app on it.

What is Laravel used for?

Laravel is mainly used to build custom web apps with PHP. It is a web framework that handles many things that are annoying to build yourself, such as routing, HTML templates and authentication.

Laravel is completely server-side, due to the fact that it runs on PHP, and focuses strongly on data manipulation and sticking to a Model-View-Controller design. A framework like React can put most of its attention on user interaction and brilliant features, but Laravel simply presents a solid foundation for you to build on ̵

1; and does it right.

Laravel is one of the best PHP web frameworks, but there are many other frameworks in different languages. Rails is another rendered server-side framework, similar to Laravel, but based on Ruby. React, Vue and Angular are all JavaScript frames on the client side but can also be configured to make the server side.

Alternatively, if your application leans towards a blog style with multiple text-based posts, you can base it on WordPress, which also runs on PHP. But Laravel does not force you to use features you do not want, it only gives you the tools to build something like WordPress yourself.

How does Laravel work?

Laravel uses a design pattern called Model-View-Controller or MVC.

The “model” represents the shape of the data that your application uses. If you have a table of users, each with a list of posts they have made, this is your model.

The “control unit” interacts with this model. If a user requests to see their post page, the controller talks to the model (often just the database) and retrieves the information. If the user wants to make a new post, the control unit updates the model. The control unit contains most of the logic for your application.

The controller uses that information to create a “view”. The view is a template with which the model can be connected to and displayed, and it can be manipulated by the control unit. The view is all the HTML components of your application.

Model-View-Controller

Laravel uses this structure to run custom apps. It uses the Blade template engine, which allows HTML to be split and managed by the controller. It all starts with routes, defined in routes/web.php, which handles HTTP requests based on the requested location. For example, the following function would run if a user requested https://yoursite.com/greeting:

Route::get('/greeting', function () {
  return view('greeting', ['name' => 'James']);
});

This route runs a function that returns a view from resources/views/. View sent data ( name variable), which it can use in the selection:




    
        

Hello, {{ $name }}

This is as simple as it gets, but a lot can happen between requesting and returning a view. Laravel supports middleware, which runs before the request is processed. You can use this to lock certain pages by checking if a user is authenticated before handling a request.

Instead of displaying a view directly, you can also forward the request to a controller, which can handle more complex logic before finally returning any resource (often a view). You can read more about the internal features of the Laravel framework in their documents.

How to get started

Laravel runs on PHP, which means that all you need is a web server like Apache or Nginx with PHP installed. You also need Composer, a dependency manager for PHP, and you need a database. MySQL works well, but PostgreSQL and SQLite are also supported.

Once dependencies are installed, you can download and install Laravel from Composer:

composer global require laravel/installer

This is technically just the Laravel installer, so you need to create a new Laravel installation with it laravel new:

laravel new blog

This creates a new directory named “blog” and installs Laravel in it. This contains a built-in .htaccess file, so all you need to do is make sure mod_rewrite is activated to activate .htaccess files and point Apache to the directory. Alternatively, if you just want to get it off the ground, you can use PHP’s built-in Artisan server by running the following command in the project directory:

php artisan serve

This starts a development server on localhost:8000. If it is running on a server, you must open that port or use SSH tunneling to access it. However, this is not a real web server, so you still want Apache or Nginx for production.


Source link