php-laravelHow do I create a controller in Laravel using PHP?
To create a controller in Laravel using PHP, you need to use the artisan
command line utility.
First, create a controller class in the app/Http/Controllers
directory:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyController extends Controller
{
//
}
Next, generate the controller using the artisan
command:
php artisan make:controller MyController
This will create the controller file and add the following code to it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
The controller class contains 8 methods: index
, create
, store
, show
, edit
, update
, destroy
.
Each of these methods is responsible for performing a specific action, like displaying a form, creating a resource, or deleting a resource.
You can add your own custom logic to these methods to create the desired functionality.
Helpful links
More of Php Laravel
- How can I use the @yield directive in PHP Laravel?
- How can I use React with PHP Laravel?
- How do I set up a Laravel worker using PHP?
- How can I set up a Laravel development environment on Windows?
- How can I use PHP, Laravel, and Vue together to create a web application?
- How do I use PHP Laravel Tinker to debug my code?
- How do I set the timezone in PHP Laravel?
- How can I use PHP and Laravel together?
- How can I find PHP Laravel jobs?
- ¿Cómo configurar PHP y Laravel desde cero?
See more codes...