1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use App\Http\Requests; |
7
|
|
|
use Silber\Bouncer\Database\Constraints\Roles; |
8
|
|
|
|
9
|
|
|
class RolesController extends Controller |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get the role overview. |
13
|
|
|
* |
14
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
15
|
|
|
*/ |
16
|
|
|
public function index() |
17
|
|
|
{ |
18
|
|
|
$data['roles'] = Roles::all(); |
|
|
|
|
19
|
|
|
return view('roles.index', $data); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Search for a specific role. |
24
|
|
|
* |
25
|
|
|
* @param Request $request |
26
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
27
|
|
|
*/ |
28
|
|
|
public function search(Request $request) |
29
|
|
|
{ |
30
|
|
|
$term = $request->get('term'); |
31
|
|
|
$data['roles'] = Roles::where('name', 'LIKE', "$term")->get(); |
|
|
|
|
32
|
|
|
return view('roles.index', $data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get a specific role. and display it. |
37
|
|
|
* |
38
|
|
|
* @param int $id the id off the role in the database. |
39
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
40
|
|
|
*/ |
41
|
|
|
public function show($id) |
42
|
|
|
{ |
43
|
|
|
$data['query'] = Roles::find($id); |
|
|
|
|
44
|
|
|
return view('roles.specific', $data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Store the new role in the database. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Http\RedirectResponse |
51
|
|
|
*/ |
52
|
|
|
public function store() |
53
|
|
|
{ |
54
|
|
|
return redirect(302)->back(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Display the form for creating a new role. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
61
|
|
|
*/ |
62
|
|
|
public function create() |
63
|
|
|
{ |
64
|
|
|
return view('roles.create'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the edit form for the selected role. |
69
|
|
|
* @param int $id The role id in the database. |
70
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
71
|
|
|
*/ |
72
|
|
|
public function edit($id) |
73
|
|
|
{ |
74
|
|
|
$data['query'] = Roles::find($id); |
|
|
|
|
75
|
|
|
return view('roles.edit', $data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Update a role in the database. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\Http\RedirectResponse |
82
|
|
|
*/ |
83
|
|
|
public function update() |
84
|
|
|
{ |
85
|
|
|
return redirect()->back(302); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Destroy a role out off the database. |
90
|
|
|
* |
91
|
|
|
* @param int $id The id in the database for the role. |
92
|
|
|
* @return \Illuminate\Http\RedirectResponse |
93
|
|
|
*/ |
94
|
|
|
public function destroy($id) |
95
|
|
|
{ |
96
|
|
|
Roles::destroy($id); |
|
|
|
|
97
|
|
|
session()->flash('message', 'Role deleted'); |
98
|
|
|
return redirect()->back(302); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.