Laravel Notes

Laravel is a PHP development framework that adheres to the MVC (Model, View and Controller) paradigm. The framework was intended to be a better alternative to Codeigniter which is a different framework. It surpassed Codeigniter though in a lot of areas. These areas include but not limited to: built-in support for authentication, localisation, models, views, sessions, routing and other mechanisms.

Furthermore, Codeigniter development has ceased to evolved. New features are non existence making this framework obsolete. As a result, Laravel is now the goto framework for PHP developers.

Here are my notes to guide me when developing web applications using Laravel. Take note however, that you need to have PHP and Composer installed.

Creating a project

> composer create-project laravel/laravel project-folder #create project.

Invoking this command will create a project with project-folder as the folder of the project. This may take a while so you need to be patient on this. Once this is done, we may now start development of our PHP web application.

 

Basic Routing

Routing is essential to applications. We may set our routes on routes/web.php:

Route::get('/', function(){
 return "Hello World.";
});

Route::get('/about', function(){
 return view('pages.about');
});

To render our view, we may have the following code on resources/views/pages/about.blade.php

This is the about page.

We may call routes pointing to some information. These are called dynamic routes. For example, to view a certain person’s information, you may add the following code to your route.

Route::get('/person/{id}', function($id){
 return "this is person: " . $id;
});

The $id parameter here is a key to identify the person. We may use this variable to access information saved to the database.

Creating Controllers

> php artisan make:controller PagesController

PascalCase on the controller name. This will create a file under: app/Http/Controllers/PagesController.php

php artisan make:controller PostsController --resource

The above command will add a controller and CRUD placeholder functions (Create, Retrieve, Update and Delete) to it.

Creating a Model

> php artisan make:model Posts -m #when you want a migration script included
php artisan tinker

 

 

 

Please follow and like us:
error