In previous lessons, I just told about theoretical concepts behind this laravel framework. Now try to understand with a code about how it works. There are multiple supporting files for us in the framework, I will discuss about it in our upcoming lessons.
Example:”Hi, This is Laravel” in routes/web.php
//File: routes/web.php
<?php Route::get ( ‘/’, function() {
return ‘Hi, This is Laravel’;
}); ?>
In laravel, your first task is to define a route to return a result whenever someone visits the route. If you create a fresh laravel application then define the route like we discussed at previous Example and then serve the site from the public directory. see the result below:
we can do the similar things by using controllers like follows:
Example: “Hi, This is Laravel” with controllers
// File: routes/web.php
<?php
Route::get(‘/’,’WelcomeController@index’); ?>
// File: app/Http/Controllers/WelcomeController.php
<?php
namespace app\Http\Controllers;
class WelcomeController
{
public function index()
{
return ‘Hi, This is Laravel’;
}
} ?>
We can print this message from database also. Here is the process of doing this:
//File: routes/web.php
<?php
Route::get(‘/’, function() {
return Hello::first()->body;
});
//File: app/Hello.php <?php
use Illuminate\Database\Eloquent\Model;
class Hello extends Model {}
//File: database/migrations/2015_07_19_010000_create_greetings_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateHelloTable extends Migration
{
public function up()
{
Schema::create(‘hello’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘body’);
$table->timestamps();
});
}
public function down()
{
Schema::drop(‘hello’);
}
}
So we see that, in laravel it is easy to write and control code easily however a coder want. There are no hectic things that you should remember because the ecosystem of tools in laravel will help you to develop a system with it’s modern coding standard.