How can I access a subfolder controller on resource Route?
I have this structure of folders:
-Controllers
--Superadmin
---PermissionController.php
PermissionController.php
<?php
namespace App\Http\Controllers\Superadmin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PermissionController extends Controller
{
}
I have tried some ways for this but still get the same error Target class was PermissionController was not found.
web.php
Route::group(['middleware' => 'auth'], function() {
Route::resource('permission', 'Superadmin\PermissionController'); // 1
Route::resource('permission', 'PermissionController'); // 2
Route::resource('permission', App\Http\Controllers\Superadmin\PermissionController::class); //3
Route::resource('permission', PermissionController::class); //4
)}
Here are all the solutions I have tried. I also added the use App\Http\Controllers\Superadmin\PermissionCotnroller when I tried solution number 4. Can't find the problem.
Try adding the namespace parameter to the group, like so:
Route::group(['namespace'=>'Superadmin','middleware'=>'auth'], function () {
Route::resource('permission', 'PermissionController'); // 2
}
Related
I'm using Laravel v8 and i tried to create an API route for admin authentication like this:
routes/api.php
Route::prefix('/admin')->name('admin.')->namespace('Admin')->group(function(){
Route::middleware('auth:admin-api')->group( function () {
Route::resource('tasks', TaskController::class);
});
Route::namespace('Auth')->group(function(){
//Login Routes
Route::post('/login','LoginController#login');
Route::post('/register','LoginController#register');
});
});
when i tried to access http://laravel.local/api/admin/login i got this following error:
Illuminate\Contracts\Container\BindingResolutionException: Target class [Admin\Auth\LoginController] does not exist. in file /Users/John/Sites/laravel/htdocs/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 875
I already created the controller file in app/Http/Controllers/API/Admin/Auth/LoginController.php
namespace App\Http\Controllers\API\Admin\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\API\BaseController as BaseController;
use App\Models\Admin;
use Validator;
class LoginController extends BaseController
{
public function login(Request $request)
{
....
}
}
You created LoginController in App\Http\Controllers\API\Admin\Auth namespace, but when you added route login in api route file, don't use the API namespace.
You should update you code.
use App\Http\Controllers\API\Admin\Auth\LoginController;
Route::prefix('/admin')->name('admin.')->group(function(){
Route::middleware('auth:admin-api')->group( function () {
Route::resource('tasks', TaskController::class);
});
Route::post('/login', [LoginController::class, 'login']);
Route::post('/register', [LoginController::class, 'register']);
});
**Target class ** does not exist. ?? Why I didn't understand
Error Is lluminate\Contracts\Container\BindingResolutionException
Target class [app\Http\Controllers\FrontEnd\IndexController] does not exist.
Illuminate\Container\Container::build
C:\xampp\htdocs\check-time.com\vendor\laravel\framework\src\Illuminate\Container\Container.php:875
I am Using Laravel 8. Environment information
Laravel version
8.47.0
Laravel locale
en
Laravel config cached
true
PHP version
8.0.6
<?php
namespace App\Http\Controllers\FrontEnd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class IndexController extends Controller
{
public function UserGuide(){
return view('FrontEnd.FrontWeb.User-Guide');
}
public function About(){
return view('FrontEnd.FrontWeb.about');
}
public function Download(){
return view('FrontEnd.FrontWeb.download');
}
public function ContectUs(){
return view('FrontEnd.FrontWeb.contact-us');
}
}
Here is My Web.php Route
<?php
use Illuminate\Support\Facades\Route;
use app\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Backend\AdminDashboardController;
use App\Http\Controllers\Backend\CategoryController;
use App\Http\Controllers\UserGuide\UserGuideController;
use App\Http\Controllers\Backend\AdminController;
use App\Http\Controllers\FrontEnd\IndexController;
// front end Route All GO Here
Route::get('/', function () {
return view('FrontEnd.FrontWeb.index');
});
Route::get('/User-Guide',[IndexController::class,'UserGuide'])->name('User.Guide');
Route::get('/about',[IndexController::class,'About'])->name('About.Page');
Route::get('/check-time-Software-download',[IndexController::class,'Download'])->name('Download.Page');
Route::get('/contact-us',[IndexController::class,'ContectUs'])->name('Contact.Us');
// Admin Route All Here
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('logout/',[AdminController::class,'logout'])->name('user.logout');
Namespaces are case-sensitive. In Laravel, the app namespace is with a lowercase a.
Method 1 :
Just import your controller in the routes file, like the following example, or use full path with controller file.
use App\Http\Controllers\IndexController;
Route::post('/about' , 'IndexController#About');
//OR
Route::get('/about', 'App\Http\Controllers\IndexController#About');
Method 2 :
Go to app/Providers/RouteServiceProvider.php and find and enable this line, as it should be commented.
protected $namespace = 'App\\Http\\Controllers';
I'am trying to use an API in laravel 5.4,and while executing in command-line 'php artisan route:list' to retreive all site routes, but unfortunately i get this error :
[ReflectionException]
Class App\Http\Controllers\Api\V1\CausesController.php does not exist
this is my Controller :
namespace App\Http\Controllers\Api\V1;
use App\Cause;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreCausesRequest;
use App\Http\Requests\Admin\UpdateCausesRequest;
use App\Http\Controllers\Traits\FileUploadTrait;
class CausesController extends Controller
{
use FileUploadTrait;
public function index()
{
return Cause::all();
}
}
and this is my api.php :
<?php
Route::group(['prefix' => '/v1', 'namespace' => 'Api\V1', 'as' => 'api.'], function () {
Route::resource('causes', 'CausesController.php');
});
Thanks.
On route resource, controller name must be "'CausesController". So, delete .php ext.
In my web.php I have the following route set up. What I was wanting to know is there something specific that I need to follow to get a sub domain of a sub domain to work?
The domain I am using is blah.blah.domain.tld
web.php:
Route::group(['domain' => '{blah}.blah.domain.tld'], function (){
Route::get('', 'DealsFrontEnd#index' );
});
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class DealsFrontEnd extends Controller
{
public function index()
{
return view('front.deals');
}
}
Too long for a comment: Try using a closure for debugging:
Route::group(['domain' => '{blah}.blah.domain.tld'], function (){
Route::get('', function() {
echo "Hello World";
});
});
Make sure you have debug enabled to make use of Laravel's error handling/reporting.
As I said in the comments, you shouldn't wrap the subdomain in brackets unless you want it to be dynamic.
{blah}.blah means it will capture anything.blah and the route variable $blah will be equal to anything.
I am new to laravel. I am trying to organise my controller by putting it inside a folder, but it doesn't seem to work.
My folder structure is like this:
/app
/Http
/Controllers
/Admin
ShowDashboard.php
My ShowDashboard.php file is like this:
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class ShowDashboard extends Controller {
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function init()
{
return 'Hi there!';
}
}
My route is like this
Route::get('/admin', 'Admin\ShowDashboard#init');
When I tred to access http://localhost:8000/admin I get the following error:
Class App\Http\Controllers\Admin\ShowDashboard does not exist
My autolaoder section:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
}
Am I missing something?
The best way to create a controller is to use the built in Laravel utility, Artisan. From a command prompt, browse to the directory your laravel project is located. For example: c:\development\htdocs\www.example.dev
At the prompt, type: php artisan make:controller admin/showDashboard --plain
This will generate a file named showDashboard.php within an admin directory under your controllers. The file will have the following code by default:
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class showDashboard extends Controller
{
//
}
Now that you have created your controller, add a method for init:
public function init()
{
return 'Hi there!';
}
Your controller will now look like this:
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class showDashboard extends Controller
{
public function init()
{
return 'Hi there!';
}
}
Now, setup your route in your routes.php as follows:
route::get('admin', 'admin\showDashboard#init');
Save your work, and launch your page. When browsing to www.example.dev/admin you should see the message: Hi there!
I hope this helps!
I don't know why this was happening, but adding this in my route fixed it.
Route::group(['namespace' => 'Admin'], function()
{
// Controllers Within The "App\Http\Controllers\Admin" Namespace
Route::get('/admin','ShowAdminDashboard#index');
});
Everything is already explained but one more try can be done by
adding controller suffix to showDashboard and run composer dump-autoload.
I think then your controller will run.
Rename your controller ShowDashboardController
php artisan make:controller Admin/ShowDashboardController
File name should be ShowDashboardController.php
I don't see anything wrong with what you posted. If you changed the namespace-to-folder mappings in composer.json, make sure you ran the 'composer dump-autoload' command.
The following code is working.. Try once
created a file ShowDashboard.php in folder admin like app/http/controller
now ,
ShowDashboard.php
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
class ShowDashboard extends Controller {
public function init()
{
return 'Hi there!';
}
}
Added Route::get('admin', 'admin\ShowDashboard#init'); in routes.php
and then run composer update on cmd..
Then run http://localhost:8000/admin .
it says.. Hi there!
Create a new controller in subfolder, for example: app/Http/Controllers/User/UserController.php
In this controller, at the end of namespace must include folder name
Like this: namespace App\Http\Controllers\User;
The important thing is under namespace must write use App\Http\Controllers\Controller;
finally in routes.php Route::get ( '/user', 'User\UserController#login' );
UserController.php contents:
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
class UserController extends Controller {
public function login() {
return 'this login';
}
}
routes.php contents:
Route::get ( '/user/login', 'User\UserController#login' );
// or use this
Route::group ( [
'namespace' => 'User'
], function () {
Route::get ( '/user/login', 'UserController#login' );
} );