Controller is a php class of many methods. In this we can do many operations like showing data, inserting data, updating data, deleting data etc. We store the controller in the folder of app / http /controllers.
Laravel has provided us a command to create the controller file, with the help of which we can create a fresh controller file.
php artisan make:controller <Controller Name>
php artisan make:controller HomeController
php artisan make:controller ProductController
php artisan make:controller CartController
php artisan make:controller OrderController
php artisan make:controller SettingController
Default controller file syntax:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
}
To display the data of the controller on the browser, the controller is called from the routes file.
Route::get('index',[HomeController::class,'index']);
Route::get('about-us',[HomeController::class,'about_us']);
Route::get('service',[HomeController::class,'service']);
Route::get('contact-us',[HomeController::class,'contact_us']);
class HomeController extends Controller
{
public function index()
{
return view('index');
}
public function about_us()
{
return view('about_us');
}
public function service()
{
return view('service');
}
public function contact_us()
{
return view('contact_us');
}
}
There are many ways to pass data from controller to view/blade file. Like compact method, with method etc.
public function index()
{
$title = 'Home Page';
return view('index',compact('title'));
}
public function index()
{
$title = 'Home Page';
return view('index')->with(['title'=>$title]);
}
public function index()
{
$data['title'] = 'Home Page';
return view('index')->with($data);
}
Invokable controller we use to perform single action. In this controller only invoke method is given then default call is.
// create invokable controller
php artisan make:controller ProductController --invokable
invokable controller default syntax:
class SingleActionController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
echo "single action controller called";
}
}
Resource controller helps in handling all http requests. The resource method is given to call the resource controller in routes. Requests like GET, POST, PUT, PATCH, DELETE can be handled with a single line.
index(), create(), store(), show(), edit(), update(), destory()
// create resource controller
php artisan make:controller ProductController --resource
how to call resource controller in routes
Route::resource('users', \App\Http\Controllers\UserController::class);
Index method is given to display all the records.
public function index()
{
echo "show users all data";
}
Create method is given to create a new record.
public function create()
{
//
}
public function store(Request $request)
{
//
}
Show method is given to show a single record.
public function show($id)
{
echo "show user single data by id";
}
public function edit($id)
{
//
}
Update method is given to update a selected record
public function update(Request $request, $id)
{
//
}
To delete the record, destroy method is given in the resource controller.
public function destroy($id)
{
//
}
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in