1:- Controller Using Artisan Command
Now, we will show you how to create simple and resource controller in laravel 8 app using artisan command.
So open your terminal and navigate to your laravel 8 app directory. After that, use the below-given command to create simple and resource controller in laravel 8 app.
Create a Simple Controller
To create simple controller in laravel 8 app by the following command:
1
|
php artisan make:controller BOOKController
|
The above command will create a simple controller file inside app/http/controllers directory. When you open it, you will look like:
1
2
3
4
5
6
7
8
9
10
11
|
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class BOOKController extends Controller
{
}
|
Create a Resource Controller
To create a Resource controller in laravel 8 app by the following command:
1
|
php artisan make:controller CRUDController --resource
|
The above command will create a resource controller file inside app/http/controllers directory. When you open it, you will look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class BOOKController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request )
{
}
/**
* Display the specified resource.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function show(Book $book )
{
}
/**
* Show the form for editing the specified resource.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function edit(Book $book )
{
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function update(Request $request , Book $book )
{
}
/**
* Remove the specified resource from storage.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function destroy(Book $book )
{
}
}
|
Note that, You saw above 2 commands to create simple controller and resource controller. Also saw that there is a slight difference in the command to create both controllers. By running the command of simple controller, a simple controller file is created. But when you execute the command to create the resource controller. Then resource controller contains bydefault index (), create (), edit (), update (), destroy () method inside it.
Create a Resource Controller with Model
To create Resource controller in laravel 8 app by the following command:
1
|
php artisan make:controller BOOKController --resource --model=book
|
The above command will create resource controller with model file. And controller file has located inside app/http/controllers directory. And Model file has been located inside app/Models directory.
If you open controller file, you will look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
Use AppModelsBook;
class BOOKController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request )
{
}
/**
* Display the specified resource.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function show(Book $book )
{
}
/**
* Show the form for editing the specified resource.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function edit(Book $book )
{
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function update(Request $request , Book $book )
{
}
/**
* Remove the specified resource from storage.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function destroy(Book $book )
{
}
}
|
If you open Model file, you will look like:
1
2
3
4
5
6
7
8
9
10
11
|
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Crud extends Model
{
use HasFactory;
}
|
2:- Routes
Now, we will show you how to define or create simple and resource rotues in laravel 8 app.
So, Navigate to your laravel 8 app directory. And open web.php file, which is placed inside routes directory.
Create Simple Routes
Create Simple routes for crud application in laravel 8 app:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersBOOKController;
Route::get( 'books' ,[ 'as' => 'books.index' , 'uses' => 'BOOKController@index' ]);
Route::post( 'books/create' ,[ 'as' => 'books.store' , 'uses' => 'BOOKController@store' ]);
Route::get( 'books/edit/{id}' ,[ 'as' => 'books.edit' , 'uses' => 'BOOKController@edit' ]);
Route::patch( 'books/{id}' ,[ 'as' => 'books.update' , 'uses' => 'BOOKController@update' ]);
Route:: delete ( 'books/{id}' ,[ 'as' => 'books.destroy' , 'uses' => 'BOOKController@destroy' ]);
Route::get( 'books/{id}' ,[ 'as' => 'books.view' , 'uses' => 'BOOKController@view' ]);
|
Create Resource Routes
Create resource routes for crud application in laravel 8 app:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersCRUDController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::resource( 'crud' , CRUDController:: class );
|
Then open terminal and run the following command on it:
php artisan route:list
The following command will display resource routes methods:
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | books | books.index | AppHttpControllersBOOKController@index | web |
| | POST | books | books.store | AppHttpControllersBOOKController@store | web |
| | GET|HEAD | books/create | books.create | AppHttpControllersBOOKController@create | web |
| | GET|HEAD | books/{book} | books.show | AppHttpControllersBOOKController@show | web |
| | PUT|PATCH | books/{book} | books.update | AppHttpControllersBOOKController@update | web |
| | DELETE | books/{book} | books.destroy | AppHttpControllersBOOKController@destroy | web |
| | GET|HEAD | books/{book}/edit | books.edit | AppHttpControllersBOOKController@edit | web |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
Note that, If you use simple routes. Then you need to define all routes in routes file. But if you use resource route. Then you just have to write the resource in front of the single route. As you saw in the above-given example.
3:- API Controller and Routes
To create resource controller by using the following command:
Create Resource Controller
To create simple controller in laravel 8 app by the following command:
1
|
php artisan make:controller APIBOOKController- resource
|
The above command will create a simple controller file inside app/http/controllers/API directory. When you open it, you will look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<?php
namespace AppHttpControllersAPI;
use AppHttpControllersController;
use IlluminateHttpRequest;
class BOOKController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request )
{
}
/**
* Display the specified resource.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function show(Book $book )
{
}
/**
* Show the form for editing the specified resource.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function edit(Book $book )
{
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function update(Request $request , Book $book )
{
}
/**
* Remove the specified resource from storage.
*
* @param AppBook $book
* @return IlluminateHttpResponse
*/
public function destroy(Book $book )
{
}
}
|
Define Resource API Route
Now, navigate to routes directory and open api.php. Then you can define resource api routes in laravel 8 app:
1
2
3
|
use AppHttpControllersAPIBOOKController;
Route::resource('book', [BOOKController::class]);
|
Conclusion
In this laravel 8 resource route, controller example, you have learned how to create resource controller and api resource controller using artisan command in laravel 8 app. And how to define resource routes and api resource routes in laravel 8 app.