Laravel Artisan How To Make Controller
--
Do you need to make a new controller in Laravel ? It is very simple, just run this command:
php artisan make:controller MyCustomController
And that’s it ! This will create an empty controller in directory app/Http/Controllers with no methods…
But I need more methods
That is why you can make a resource controller, a controller who has all required methods to process your logic and your data, like: store, update, create, index, delete, ecc… Just run this command:
php artisan make:controller MyCustomController -r
This will make a controller in app/Http/Controllers with all required methods, like this:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class MyCustomController 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)
{
//
}
}
Now you have your controller! Just write your logic in it. But wait…
It Doesn’t Work ?
Yes because you have to add the routes manually to web.php or wherever you need it… so you will need to write things like:
Route::get(‘/mycontroller’…