I have this route included in my web.php in my routes folder.
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'filemanager'], function () {
\UniSharp\LaravelFilemanager\Lfm::routes();
});
});
My issue is whenever I run php artisan route:list I get the following error:
Class App\Http\Controllers\Admin\UniSharp\LaravelFilemanager\Controllers\LfmController does not exist
The Lfm controller is inside my vendor folder and I've been scouting around the internet for a solution and applied many different ways for the route change and the only way I got it to work is by using the route provided by default from the package. But if I use that, I use lose my admin authentication I'm hoping someone could give me some insight as to what I'm doing wrong here?
I would really appreciate that! Thank you in advance.
I came up with this solution. I am using Laravel 5.4
Instead of putting the code in routes/web.php, put it in RouteServiceProvider, so the default namespace is not set yet;
protected function mapWebRoutes()
{
Route::group(['prefix' => 'laravel-filemanager', 'middleware' => ['web', 'auth']], function () {
\UniSharp\LaravelFilemanager\Lfm::routes();
});
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
For laravel 5.6 and above,
Go to config/lfm.php and set 'use_package_routes' to false.
In routes/web.php use the following routes (this is an example, might change adhering to your prefix etc.):
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware' => 'admin', 'as' => 'admin.'], function () {
Route::get('/filemanager', '\UniSharp\LaravelFilemanager\Controllers\LfmController#show')->name('show');
Route::any('/filemanager/upload', '\UniSharp\LaravelFilemanager\Controllers\UploadController#upload')->name('unisharp.lfm.upload');
Route::get('/filemanager/errors', '\UniSharp\LaravelFilemanager\Controllers\LfmController#getErrors')->name('getErrors');
Route::get('/filemanager/jsonitems', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#getItems')->name('getItems');
Route::get('/filemanager/move', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#move')->name('move');
Route::get('/filemanager/domove', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#move')->name('domove');
Route::get('/filemanager/newfolder', '\UniSharp\LaravelFilemanager\Controllers\FolderController#getAddfolder')->name('getAddfolder');
Route::get('/filemanager/folders', '\UniSharp\LaravelFilemanager\Controllers\FolderController#getFolders')->name('getFolders');
Route::get('/filemanager/crop', '\UniSharp\LaravelFilemanager\Controllers\CropController#getCrop')->name('getCrop');
Route::get('/filemanager/cropimage', '\UniSharp\LaravelFilemanager\Controllers\CropController#getCropimage')->name('getCropimage');
Route::get('/filemanager/cropnewimage', '\UniSharp\LaravelFilemanager\Controllers\CropController#getNewCropimage')->name('getCropimage');
Route::get('/filemanager/rename', '\UniSharp\LaravelFilemanager\Controllers\RenameController#getRename')->name('getRename');
Route::get('/filemanager/resize', '\UniSharp\LaravelFilemanager\Controllers\ResizeController#getResize')->name('getResize');
Route::get('/filemanager/doresize', '\UniSharp\LaravelFilemanager\Controllers\ResizeController#performResize')->name('performResize');
Route::get('/filemanager/download', '\UniSharp\LaravelFilemanager\Controllers\DownloadController#getDownload')->name('getDownload');
Route::get('/filemanager/delete', '\UniSharp\LaravelFilemanager\Controllers\DeleteController#getDelete')->name('getDelete');
Route::get('/filemanager/demo', '\UniSharp\LaravelFilemanager\Controllers\DemoController#index')->name('getDelete');
});
Related
I have a path in Laravel it is like subdomain.mydomain.com/admin/login
I am trying to call
subdomain.mydomain.com and need to get the login page straight.
Currently, it's not working
This is the function I am using in routerserviceprovider.php
protected function mapAdminRoutes()
{
Route::middleware('subdomain.mydomain.com')
->prefix('admin')
->namespace($this->namespace)
->group(base_path('routes/admin.php'));
}
and in admin.php there is a resource group shows like this:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
//Login Routes...
Route::view('login','admin.login');
});
can anyone help with this?
Add following route
Route::get('/',function(){ return view('login.index'); })->name('admin.login');
i hope it helps
I am developing web application with laravel 5.2.
What i need is i have some off account that distinguished by role. and i have to role that can access 1 route but other role cannot access. i have browsing and i have done everything that i found like
Route::group(['prefix' => '/', 'middleware' => ['role:user_a','role:user_b']], function(){someroute}
Route::group(['prefix' => '/', 'middleware' => ['role:user_a|role:user_b']], function(){someroute}
Route::group(['prefix' => '/', 'middleware' => ['role:user_a,role:user_b']], function(){someroute}
no one work. i dont know how to make my single route can be accessed by 2 role but disable for other role
You can create a middleware named role, read more about middleware in docs here
The handle method of middleware will be as:
public function handle($request, Closure $next)
{
if (auth()->user()->role('b')) {
return redirect('home');
}
// else users of other roles can visit the page
return $next($request);
}
Then you can use it in your route file as:
Route::group(['middleware' => 'role'], function () {
// someroute
});
i think you cant do this, but you can use this way.
Route::group(['prefix'=>'/', 'middleware' =>['role:user_a','role:user_b']],function(){
Route::group(['prefix'=>'userAorB', 'middleware' =>['role:user_a|role:user_b']],function(){ Routes });
Route::group(['prefix'=>'userAANDB', 'middleware' =>['role:user_a,role:user_b']],function(){ Routes });
})
I have 2 user roles which is superadmin and admin
I don't want admin to access of Settings Page.
I am not sure if this is the proper way.
So, here's my SettingsController.php
class SettingsController extends Controller {
public function index() {
if(Auth::user()->roles == 0) {
return redirect(url()->previous());
} else {
return view('settings.index');
}
}
}
As you can see if the roles is 0. I redirect the user to the last page they're in. I also tried to use return back();
web.php (routes)
<?php
Route::get('/', ['uses' => 'UsersController#index']);
Route::post('login', ['uses' => 'UsersController#login']);
Route::group(['middleware' => ['auth']], function() {
Route::get('logout', ['uses' => 'UsersController#destroy']);
Route::get('upline', ['uses' => 'UplinesController#index']);
Route::get('upline/create', ['uses' => 'UplinesController#create']);
Route::post('upline', ['uses' => 'UplinesController#store']);
Route::delete('upline/destroy/{id}', ['uses' => 'UplinesController#destroy']);
Route::put('upline/update/{id}', ['uses' => 'UplinesController#update']);
Route::get('upline/getdownlines/{id}', ['uses' => 'UplinesController#getDownlines']);
Route::get('downline', ['uses' => 'DownlinesController#index']);
Route::post('downline', ['uses' => 'DownlinesController#store']);
Route::delete('upline/destroy/{id}', ['uses' => 'DownlinesController#destroy']);
Route::put('downline/update/{id}', ['uses' => 'DownlinesController#update']);
Route::get('bonus', ['uses' => 'BonusController#index']);
Route::post('bonus/csv', ['uses' => 'BonusController#fileUpload']);
Route::get('settings', ['uses' => 'SettingsController#index']);
});
I have a 2nd question. Can I limit admin using middleware? If yes, how?
Any help would be appreciated.
Maybe the second option, "Limiting admin with middleware".
So you can try something like;
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
Route::get('/', 'DownlinesController#update');
});
Then
Route::group(['prefix' => 'super', 'middleware' => 'auth'], function () {
Route::get('/', 'UplinesController#index');
});
As #michael s answer suggests use middleware, his answer fails to demonstrate on how to do it (mine too, I just added more text).
Note: Laravel is big because of its documentation, USE IT!
You have 2 (or more options):
parameterized middleware
2 distinctive middlewares (one for admin, another for superadmin)
Note: use artisan to generate middleware from stubs, $ php artisan make:middleware MyNewShinyMiddleware
parametrized middleware (my pick)
Head to documentation and check out this.
Example shows exactly your problem.
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) { //implement hasRole in User Model
// Redirect...
// (use named routes to redirect or do 401 (unauthorized) because thats what is going on!
// abort(401) // create view in /views/errors/401.blade.php
// return redirect()->route('home');
}
//success user has role $role, do nothing here just go to another "onion" layer
return $next($request);
}
2 distinctive middlewares
simply create two middlewares and hardcode your checking routine of roles
(same as you do in your controller sample) except use $request->user()...
(routes) web.php
Route::group(['middleware' => 'role:admin'], function () {...} //parametrized
Route::group(['middleware' => 'checkRoleAdmin'], function () {...}
Route::group(['middleware' => 'checkRoleSuper'], function () {...}
Note: role, checkRoleAdmin and checkRoleSuper are "named" middlewares and you need to register them in kernel.php
Another way is yo use gates or policies which make the best sense, since you are trying to limit user. Read more here.
I use middleware based ACL for really simple projects (like one admin and no real users).
I use gates based ACL for medium projects (1-2 roles).
I use policies based ACL for "huge" projects (many roles, many users).
Also consider looking at https://github.com/Zizaco/entrust
Is there a way in Lumen to prefix all of my routes?
The thing is that I'm versioning my API via URI and for every group that I create I have to set the prefix to 'v1/*' like:
$app->group(['prefix' => 'v1/students/', 'namespace' => 'App\Http\Controllers\Students\Data'], function () use ($app) {
$app->get('/', 'StudentController#get');
$app->get('/{id}', 'StudentController#getByID');
});
Apparently, route groups in Lumen do not inherit any settings, which was intentional to keep the router simpler and faster (see comment here).
Your best bet will probably be to create a route group per version, in order to define a base prefix and controller namespace for that version. But, your individual routes inside those route groups will need to be slightly more verbose. Example shown below:
// creates v1/students, v1/students/{id}
$app->group(['prefix' => 'v1', 'namespace' => 'App\Http\Controllers'], function () use ($app) {
$app->get('students', 'Students\Data\StudentController#get');
$app->get('students/{id}', 'StudentController#getByID');
});
// creates v2/students, v2/students/{id}, v2/teachers, v2/teachers/{id}
$app->group(['prefix' => 'v2', 'namespace' => 'App\Http\Controllers'], function () use ($app) {
$app->get('students', 'Students\Data\StudentController#get');
$app->get('students/{id}', 'Students\Data\StudentController#getByID');
$app->get('teachers', 'Teachers\Data\TeacherController#get');
$app->get('teachers/{id}', 'Teachers\Data\TeacherController#getByID');
});
You could prefix all routes in your /bootstrap/app.php.
Currently, there should be something like
# may be slighlty different, since I typed this from memory
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__ . '/../routes/web.php';
});
As you can see, this loads the web.php file and makes the $router variable available.
You can rewrite this to load any php file in the routes directory and prefix all those routes via
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
// load all files in routes directory, prefix all of them
$globalPrefix = "/v1";
$router->group(["prefix" => $globalPrefix], function($router) {
$routes = glob(__DIR__ . '/../routes/*.php');
foreach ($routes as $route) require $route;
});
});
As you can see, all required routes are wrapped in $router->group with your application wide route prefix.
I want to create pages for admin and I want to use dashboard in localhost:8000/admin url...but the page is showing a display Not Found Error.
Please tell me what should I do to resolve this
You need somethig like this in your /app/Http/routes.php
Route::group(['prefix' =>'admin', 'namespace' => 'admin', 'middleware' => 'auth'], function() {
Route::get('/', 'AdminController#index');
// .....
});