Basic controllers
- Move the logic (now in routes file routes/web.php) to controllers
- Controllers are classes that are in charge of the grunt work of preparing the data to pass to the views
- Make a new controller class with the terminal command
php artisan make:controller Admin/RecordController
$ php artisan make:controller Admin/RecordController
Controller created successfully.
1
2
2
NAMING CONVENTIONS
The name of a controller equals the singular "object" name, starting with a capital letter (e.g. Record
), followed by Controller
REMARK
Several courses/programmers use the plural noun of the "object" in the controller name (e.g. RecordsController
), although this is in contrast to the approach used in the official Laravel documentation
- Open the controller Admin/RecordController.php (located in folder app/Http/Controllers)
- Namespacing is (automatically) taken care of
- The necessary imports (the statements with the keyword
use
) are included
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RecordController extends Controller
{
//
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
REMARK
Notice the use of backslashes (\
) in the namespace
and use
statements
- Migrate all the code of the (anonymous function in the) 'admin/records' route to a new method
index()
in this controller- Add a new, fourth record to ensure that this approach will work
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RecordController extends Controller
{
public function index()
{
$records = [
'Queen - Greatest Hits',
'The Rolling Stones - Sticky Fingers',
'The Beatles - Abbey Road',
'The Who - Tommy'
];
return view('admin.records.index', [
'records' => $records
]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- Call this method
index()
in the 'admin/records' route in the routes/web.php file- The second argument of the
Route::get()
method is a string, containing the namespace (starting fromApp\Http\Controllers
) and name of the controller (Admin\RecordController
), followed by@
and the name of the method in that controller that has to be called (index
)
- The second argument of the
Route::prefix('admin')->group(function () {
Route::redirect('/', 'records');
Route::get('records', 'Admin\RecordController@index');
});
1
2
3
4
2
3
4
- Check the result in a browser by visiting http://vinyl_shop.test/admin/records
php artisan make:controller
- Artisan is the command-line interface included with Laravel, which assists in building Laravel applications
TIPS
- When you type
php artisan
orphp artisan list
in your terminal, you get a list of all the available Artisan commands - In order to get help (available arguments and options) on a specific command, precede the name of the command with
help
$ php artisan help make:controller
Description:
Create a new controller class
Usage:
make:controller [options] [--] <name>
Arguments:
name The name of the class
Options:
-m, --model[=MODEL] Generate a resource controller for the given model.
-r, --resource Generate a resource controller class.
-i, --invokable Generate a single method, invokable controller class.
-p, --parent[=PARENT] Generate a nested resource controller class.
--api Exclude the create and edit methods from the controller.
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output,
2 for more verbose output and 3 for debug
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Commit "Opdracht 1: <= Basic controllers"
- Execute the following commands in a terminal window:
git add .
git commit -m "Opdracht 1: <= Basic controllers"
git push
1
2
3
2
3