Laravel

Getting to Know Routing and Controllers in Laravel

How a URL finds the code that answers it, and when to reach for a controller.

C Codesakura 2分 コメント0件

Getting to Know Routing and Controllers in Laravel
目次

Every page starts with a route: a rule that connects a URL to code. In this part we write our first route and move its logic into a controller.

Your first route

Open routes/web.php and add:

Route::get('/hello', fn () => 'Hello from Laravel');

Using a controller

Once the logic grows, move it into a controller so the routes file stays short.

php artisan make:controller ProductController
Route::get('/products', [ProductController::class, 'index']);

URL parameters

The part of a URL in curly braces is passed to the controller. If it is type-hinted as a model, Laravel looks it up in the database for you.

シリーズの一部

Laravel from Scratch
  1. 1Setting Up Your First Laravel Project
  2. 2Getting to Know Routing and Controllers in Laravel
  3. 3Building a REST API with Laravel from Scratch
共有

コメント0件

コメントするにはログインしてください。 ログイン

関連チュートリアル