Laravel 8 Admin controller don't work - in what problem? - laravel

I have this route:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['web', 'auth']], function () {;
Route::get('/', function () {
return view('backend.app');
})->middleware(['auth'])->name('dashboard');
Route::get('/news', 'News#index');
});
use App\Http\Controllers\News;
Route::get('/news', [News::class, 'index']);
Route::get('/news/{id}', [News::class, 'show']);
I need to open link /admin/news - but I have error: Target class [Admin\News] does not exist.
News class for admin:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
class News extends Controller
{
//
public function __construct()
{
}
public function index()
{
var_dump('test');
}
}
Can you help me: in what problem in my case?

You're mixing route definitions between the 'older' Laravel 7 style and the newer Laravel 8 style. Pick one and stick to it. If you've upgraded a project from Laravel 7 to Laravel 8, consider refactoring to keep things consistent.
As you have two News controllers, you either want to use the FQN (Fully Qualified Name) of each or alias one or both of your controllers.
use App\Http\Controllers\News;
use App\Http\Controllers\Admin\News as AdminNews; // aliased
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['web', 'auth']], function () {
Route::get('/', function () {
return view('backend.app');
})->middleware(['auth'])->name('dashboard');
Route::get('/news', [AdminNews::class, 'index']);
});
Route::get('/news', [News::class, 'index']);
Route::get('/news/{id}', [News::class, 'show']);

Related

Laravel add custom middleware to route group

in my web application i have admin panel and i'm trying to make access to users that they have admin role by this code:
namespace App\Http\Middleware;
use Closure;
class CheckUserAdminRole
{
public function handle($request, Closure $next)
{
if (auth()->check()) {
if (auth()->check() && !auth()->user()->hasRole('admin')) {
auth()->logout();
return redirect(route('system.messages','userIsNotAdmin'));
}
}
return $next($request);
}
}
and in my routs i have this route group:
Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web'], 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
$this->get('panel', 'AdminController#index');
});
my kernel:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
...
\App\Http\Middleware\CheckUserAdminRole::class,
];
now when i add my middleware as CheckUserAdminRole to route group like with this code:
Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web','CheckUserAdminRole'], 'prefix' => 'dashboard'], function () {
i get this error:
Class CheckUserAdminRole does not exist
this codes couldn't resolve my problem:
php artisan route:clear
php artisan cache:clear
php artisan config:clear
composer dump-autoload
Instead of registering your middleware in the $middleware array, you should register it in $routeMiddleware like so:
protected $routeMiddleware = [
...
'checkAdmin' => \App\Http\Middleware\CheckUserAdminRole::class,
];
Note: registering a middlware in the $middleware array results in it being executed for every request and therefore is not applicable on specific routes.
Then you can use it in your routes with the checkAdmin name:
Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web','checkAdmin'], 'prefix' => 'dashboard'], function () {
Source
You can also use middleware and route group together easily like so:
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function()
{
//All the routes that belongs to the group goes here
Route::get('dashboard', function() {} );
});
Here's a simple fix without touching the Kernel file and taking advantage of the Policy. The accessAdmin is a function created inside Policy file.
Route::group(['namespace' => 'Dashboard', 'middleware' => 'can:accessAdmin, App\User', 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
$this->get('panel', 'AdminController#index');
});
You can try middleware with prefix and groups.
Route::middleware(['Auth'])->prefix('api/')->group(function() {
Route::group(['prefix' => 'review/'], function () {
Route::get('/', 'User\Controllers\Api\UserController#getUserReviews');
});
});
Hope its helps

Is it possible use controller with different namespace in route group ?

Is it possible use controller with different namespace in route group ?
Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
Route::get('/', ['namespace'=>'Controllers','uses'=>'SiteController#dashobard']);
Route::get('posts', 'PostsController#index');
});
As #TimLewis has mentioned in the comments it is possible.
(Assuming the full namespace for SiteController is App\Http\Controllers) The following should work:
Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
Route::get('/', '\App\Http\Controllers\SiteController#dashboard');
Route::get('posts', 'PostsController#index');
});
However, it would make more sense to split the routes up:
Route::group(['prefix' => 'dashboard'], function () {
Route::get('/', 'SiteController#dashboard');
Route::group(['namespace' => 'admin'], function () {
Route::get('posts', 'PostsController#index');
});
});
Hope this helps!

Remove web middleware in laravel 5.2

I know web middleware group is assigned to every route now. But can someone tell me how to remove if for specefic route? I tried:
class HomeController extends Controller{
public function __construct(){
$this->middleware('web',['except'=>[
'index',
]]);
}
}
And it doesn't work.
Web middleware is now applied to all routes in routes.php. This happens in the RouteServiceProvider map function.
If you have an api for example which should not use web middleware you can go with something like this
public function map(Router $router)
{
$this->mapWebRoutes($router);
$this->mapApiRoutes($router);
}
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}
protected function mapApiRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'api',
], function ($router) {
require app_path('Http/routes-api.php');
});
}
Now every route in routes.php has web middleware and everything in routes-api.php the api middleware

Laravel always takes me to login page

I'm using Laravel 5's auth module for my app. However after I created the auth functions with php artisan make:auth I can only access two paths:/login and /register no matter how I add routes in the routes.php. All other paths redirect me to the login page. How can I enable users to access certain paths without logging? Thanks.
routes.php:
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/patient', 'HomeController#registerPatient');
Route::get('test', 'HomeController#index');
Route::group(array('before' =>'auth'), function()
{
Route::get('about', array('as' => 'about','uses' => 'HomeController#about'));
}
);
Route::group(array('before' =>'auth'), function()
{
Route::get('/physician', array('as' => 'physician','uses' => 'HomeController#registerPhysician'));
}
);
Can you share your controller code? i mean do you have the auth middleware in ur controller construct?
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index');
Route::get('/patient', 'HomeController#registerPatient');
Route::get('test', 'HomeController#index');
});
Route::group(array('before' =>'auth'), function()
{
Route::get('/physician', array('as' => 'physician','uses' => 'HomeController#registerPhysician'));
Route::get('about', array('as' => 'about','uses' => 'HomeController#about'));
}
);
anyway check ur construct method in the homecontrollerand apply the auth middlewere only to certain methods
exemple
public function __construct()
{
$this->middleware('auth', ['except' => [
'about',
'index',
]]);
}
}

Laravel 5.2 - Route authentication

I create the auth class from laravel and now i would like to know how i can put the /register page only for users who login in system?
my routes are:
Route::group(['middleware' => ['web']], function () {
//
Route::get('/', 'Auth\AuthController#getLogin');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
//Route::get('/register', 'Auth\AuthController#getRegister');
});
For authenticate url just add a auth middleware, that will work after logged in a user. Follow the code below:
Route::group(['middleware' => ['web','auth']], function () {
Route::get('/register', 'YourController#getRegister');
});
You have to use a middleware to filter the users.
In the case of authentication, there is a built-in middleware
called 'auth'
You can filter by middlewares in group as AnowarCst showed you
or in single routes like this:
Route::get('/register', [
'middleware' => 'auth',
'yourController#yourFunction'
]);
Read the docs to better understand
MIDDLEWARE.
Don't be scared, it's more simple than how it looks. :)
I solved the problem with this:
public function __construct()
{
$this->middleware('guest', ['except' => ['logout', 'register', 'showRegistrationForm']]);
$this->middleware('auth', ['only' => ['register', 'showRegistrationForm']]);
}
And my routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'Auth\AuthController#getLogin');
});
Route::group(['middleware' => 'web'], function () {
Route::Auth();
Route::get('/dashboard', 'HomeController#index');
});
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/register', 'Auth\AuthController#showRegistrationForm');
});
Thanks guys.

Resources