DEV Community

Bijay Kumar Pun
Bijay Kumar Pun

Posted on

Laravel Controllers

Exceprts from the book Laravel: Up and Running by Matt Stauffer

Requests can come to applications not only via routes, but also cron jobs, Artisan command-line calls, queue jobs etc.
Controller's primary job is to capture the intent of an HTTP request and pass it on the rest of the application.

To create controller, run:
php artisan make:controller <controller-name>

This creates a controller class inside app/Http/Controllers/.
Additionally, the --resource option can be used to autogenerate methods for basic resource routes like create() and update()

Artisan's make namespace provides a lot tools for generating skeleton files for a variety of system files.

A common controller method example:

//SomeController.php
public function index(){
return view('tasks.index')
->with('tasks',Tasks:all());
}

The above can be used as:
//route/web.php
Route::get('/','SomeController@index');

This loads the resources/views/tasks/index.blade.php or resources/views/tasks/index.php and passes it a single variable named tasks which contains the result of the Task::all() Eloquent method.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay