Laravel Controllers: Basic Controllers
Basic Controllers
Defining Controllers
<?php namespace App\Http\Controllers; use App\User; use App\Http\Controllers\Controller; class UserController extends Controller
{ /**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/ public function show($id)
{ return view('user.profile', ['user' => User::findOrFail($id)]); } } Controllers & Namespaces
Since the RouteServiceProvider loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Http\Controllers portion of the namespace.
If you choose to nest your controllers deeper into the App\Http\Controllers directory, use the specific class name relative to the App\Http\Controllers root namespace.
Single Action Controllers
<?php namespace App\Http\Controllers; use App\User; use App\Http\Controllers\Controller; class ShowProfile extends Controller
{ /**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/ public function __invoke($id)
{ return view('user.profile', ['user' => User::findOrFail($id)]); } } When registering routes for single action controllers, you do not need to specify a method.
Semantic portal