decided not to use it ready, but to make an ideal administrator on the project.
And at the very beginning there is a question. You can somehow in routes / routes indicate that the path / admin
is only for “Favorites” and that it does not let and in all his submarshrutes of ordinary users?
And then I have one thought yet: in each method of each class of admins constantly prescribe a condition for checking the status of the user. But this is a bad code.
If it is not prescribed in the roots, then tell me at least the optimal solution.
Answer 1, Authority 100%
First create Middleware Admin
php artisan make: middleware admin
then add this code in Model User
// is admin
Public Function Isadmin ()
{
Return $ this- & gt; is_admin; // Field is_admin in table Users
}
Next Open Middleware Admin and add this code
// Check the user
if (auth :: check () & amp; & amp; auth :: user () - & gt; isadmin () == true)
{
Return $ Next ($ Request);
}
RETURN REDIRECT ('/');
in App \ http \ kernel.php in Protected $ RoutemidDleWare Add our middleware
'admin' = & gt; \ app \ http \ middleware \ admin :: class,
And finally in ROTE
Route :: Group (['Middleware' = & gt; 'admin', 'prefix' = & gt; 'admin '], FUNCTION () {
// only for admin
});
Answer 2, Authority 38%
Hello, I know this topic created a year ago, but I ran into the same and two previous comments did not work. I had to do it yourself.
Laravel 5.5 :
Create registration \ Authorization: PHP Artisan Migrate: Auth
Create a filtering mechanism:
In Terminal: PHP Artisan Make: Middleware Access
Open the App \ http \ middleware \ access.php
Add:
public function handle ($ request, closure $ next, $ role)
{
Switch ($ Role) {
Case 'Admin':
If (! \ AUTH :: User ()) {
if ($ Request- & gt; ajax ())
Return Response ('Access Denied') - & gt; setstatuscode (403);
ABORT (404);
} ELSE.
If (! $ Request- & gt; user () - & gt; is_admin) {
if ($ Request- & gt; ajax ())
Return Response ('Access Denied') - & gt; setstatuscode (403);
ABORT (404);
}
Break;
Default:
Return Response ('Access Denied') - & gt; setstatuscode (403);
Break;
}
Return $ Next ($ Request);
}
Open App \ http \ kernel.php
In the variable $ ROUTEMIDDLEWARE
with an array write:
'access' = & gt; \ APP \ HTTP \ Middleware \ Access :: Class,
Open App / user.php
In the variable $ hidden with an array write: 'is_admin'
Open Routes / Web.php
At the very end insert:
Route :: Group (['Middleware' = & gt; 'auth', 'middleware' = & gt; 'Access : admin '], function () {
// here continue your creation
Route :: Get ('Dashboard', Function () {Echo "Hello World";});
});
Open Database / Migrations / 2014_10_12_000000_create_users_table.php
and write down a new column in the table, namely:
$ table- & gt; biginteger ('is_admin') - & gt; default (0);
Save and now in the Terminal PHP Artisan Migrate: Reset
We delete all the migrated tables, and now migrate the new PHP Artisan Migrate
Register the user.
Now go to your database and look at the ‘Users’ table appeared column is_admin with the value of 0 it means that you are not an admin, whatever you become admin 1
Checking Log by link MySite.com/Dashboard If you see Hello World So you Admin If not then you are not admin.
Answer 3
Route :: Group (['prefix' = & gt; 'admin', 'middleware' = & gt; 'auth '], FUNCTION () {
}