Key Highlights
The Laravel homepage boasts a "clean and classy" framework, freeing developers from convoluted code. Let's test this by building a simple TODO application.
Database Migrations
First, we define the database schema. A single table with five columns (ID, title, description, created_at, updated_at) suffices. Laravel's migrations simplify database updates. The migration file looks like this:
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTodoTable extends Migration { public function up() { Schema::create('todos', function (Blueprint $table) { $table->increments('id'); $table->string('title', 20); $table->text('description'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('todos'); } }
up()
executes the migration, and down()
reverses it.
The Model
Laravel's MVC architecture requires a model for database interaction. Our simple table needs a straightforward model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Todo extends Model { protected $fillable = ['title', 'description']; public $timestamps = true; }
Laravel automatically links Todo
to the todos
table. Eloquent, Laravel's ORM, facilitates database object manipulation. $timestamps = true
automatically updates created_at
and updated_at
.
The Controller
The controller houses the application logic:
The controller with five actions (methods):
<?php namespace App\Http\Controllers; use App\Models\Todo; use Illuminate\Http\Request; class TodoController extends Controller { public function list() { $todos = Todo::all(); return view('list', compact('todos')); } public function view($id) { $todo = Todo::find($id); return view('view', compact('todo')); } public function delete($id) { $todo = Todo::find($id); $todo->delete(); return view('deleted'); } public function new() { return view('add'); } public function add(Request $request) { $validatedData = $request->validate([ 'title' => 'required|max:20', 'description' => 'required', ]); Todo::create($validatedData); return view('success'); } }
The code is clear. Eloquent simplifies database access (Todo::all()
). action_add()
uses request validation.
The View (Example: List)
Laravel's Blade templating engine creates clean views. The list
view:
<h2>Todo List</h2> <p>{{ link_to_route('todo.new', 'Add new todo') }}</p> <ul> @foreach ($todos as $todo) <li>{{ link_to_route('todo.view', $todo->title, [$todo->id]) }} - {{ link_to_route('todo.delete', 'Delete', [$todo->id]) }}</li> @endforeach </ul>
Conclusion
Building this simple application demonstrates Laravel's ease of use and code readability. The framework lives up to its "clean and classy" claim.
(Note: The provided code snippets are simplified examples and may require adjustments for a fully functional application. Error handling and more robust features would be needed in a production environment.)
The above is the detailed content of Laravel: Is It Really Clean and Classy?. For more information, please follow other related articles on the PHP Chinese website!