Up until now, everything I’ve shared here has been entirely abstract. What about the code, you
ask? Let’s dig into a simple application (Example 1-1) so you can see what working with
Laravel day-to-day is actually like.
<?php
Route::get('/', function() {
return 'Hello, World!';
});
The simplest possible action you can take in a Laravel application is to define a route and
return a result any time someone visits that route. If you initialize a brand new Laravel
application on your machine, define the route in Example 1-1, and then serve the site from the
public directory, you’ll have a fully functioning “Hello, World” example (see Figure 1-1).
Hello, World!
Figure 1-1. Returning “Hello, World!” with Laravel
It looks very similar to do the same with controllers, as you can see in Example 1-2.
<?php
Route::get('/', 'WelcomeController@index');
// File: app/Http/Controllers/WelcomeController.php
<?php
namespace app\Http\Controllers;
class WelcomeController
{
public function index()
{
return 'Hello, World!';
}
}
And if we’re storing our greetings in the database, it’ll also look pretty similar (see
Example 1-3).
<?php
Route::get('/', function() {
return Greeting::first()->body;
});
// File: app/Greeting.php
<?php
use Illuminate\Database\Eloquent\Model;
class Greeting 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 CreateGreetingsTable extends Migration
{
public function up()
{
Schema::create('greetings', function (Blueprint $table) {
$table->increments('id');
$table->string('body');
$table->timestamps();
});
}
public function down()
{
Schema::drop('greetings');
}
}
Example 1-3 might be a bit overwhelming, and if so, just skip over it. We’ll learn about
everything that’s happening here in later chapters, but you can already see that with just a few
lines of code, we’ve set up database migrations and models and pulled records out. It’s just
that simple.
ask? Let’s dig into a simple application (Example 1-1) so you can see what working with
Laravel day-to-day is actually like.
Example 1-1. “Hello, World” in routes/web.php
// File: routes/web.php<?php
Route::get('/', function() {
return 'Hello, World!';
});
The simplest possible action you can take in a Laravel application is to define a route and
return a result any time someone visits that route. If you initialize a brand new Laravel
application on your machine, define the route in Example 1-1, and then serve the site from the
public directory, you’ll have a fully functioning “Hello, World” example (see Figure 1-1).
Hello, World!
Figure 1-1. Returning “Hello, World!” with Laravel
It looks very similar to do the same with controllers, as you can see in Example 1-2.
Example 1-2. “Hello, World” 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 'Hello, World!';
}
}
And if we’re storing our greetings in the database, it’ll also look pretty similar (see
Example 1-3).
Example 1-3. Multigreeting “Hello, World” with database access
// File: routes/web.php<?php
Route::get('/', function() {
return Greeting::first()->body;
});
// File: app/Greeting.php
<?php
use Illuminate\Database\Eloquent\Model;
class Greeting 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 CreateGreetingsTable extends Migration
{
public function up()
{
Schema::create('greetings', function (Blueprint $table) {
$table->increments('id');
$table->string('body');
$table->timestamps();
});
}
public function down()
{
Schema::drop('greetings');
}
}
Example 1-3 might be a bit overwhelming, and if so, just skip over it. We’ll learn about
everything that’s happening here in later chapters, but you can already see that with just a few
lines of code, we’ve set up database migrations and models and pulled records out. It’s just
that simple.
No comments:
Post a Comment