How to put controller inside folder in laravel 5.1.3? - php

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' );
} );

Related

Illuminate\Contracts\Container\BindingResolutionException Target class [app\Http\Controllers\FrontEnd\IndexController] does not exist

**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';

Laravel can not import class from correct file, it looks in controllers

I tried to separate additional classes into files gathered in the App\Core directory. I have already tried different solutions for the problem and nothing has worked for me. I managed to create one test class static SearchProcess placed in the SearchProcess.php file. So the full path looks like App\Core\SearchProcess.php.
The file contains:
<?php
namespace App\Core;
class SearchProcess
{
public static function method_1(string $arg)
{
//Does things...
}
public static function method_2(string $arg)
{
//Does different things...
}
}
The file is 'registered' in the config\app.php in the section aliases:
'aliases' => [
//Custom aliases...
'SearchProcess' => App\Core\SearchProcess::class,
],
Finally I import it as:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
// use SearchProcess;
use App\Core;
class SearchController extends Controller
{
public function search(Request $request)
{
//Does before staf...
$search_value = SearchProcess::method_1($search_value_raw);
//Does after staf...
}
}
I run composer dump-autoload afterwards, but it does not change anything.
I get an error:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'App\Http\Controllers\SearchProcess' not found
Moreover I do not know why it looks for the file in the controllers directory?
// use SearchProcess;
use App\Core\SearchProcess;

laravel is giving error on custom controller used in routes

I have a simple in routes/web.php file
Route::get(Config::get('constants.ADMIN_PATH') . '/categories', 'AdminControllers\AdminPagesController#index');
I have made a folder AdminControllers and inside that there is a controller named AdminPagesController but i am getting error as
Class App\Http\Controllers\AdminControllers\AdminPagesController does not exist
Whereas i looked into the same folder and class exist. Here is my class code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Change you namespace to
namespace App\Http\Controllers\AdminControllers;
Laravel will resolve controllers based on your name spacing, not on your directory structure.
You should specify the namespace correctly, change it to:
namespace App\Http\Controllers\AdminControllers; // <------- correct this namespace
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Hope this helps!
If you choose to nest your controllers deeper into the **
App\Http\Controllers
** directory, use the specific class name relative to the
App\Http\Controllers
root namespace.
namespace App\Http\Controllers\AdminControllers;

Class does not exist for laravel routes

Laravel 5.1
This seems strange to me:
Route::group([
'middleware'=>['auth','acl:view activity dashboard'],
'prefix' => 'api/v1'
], function(){
Route::controller('investment-transactions', 'Api\V1\Investments\InvestmentTransactionsController');
Route::controller('investment-transactions/{offeringID}', 'Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering');
});
Seems pretty normal to me, the controller:
namespace App\Http\Controllers\Api\V1\Investments;
use App\Brewster\Models\Company;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvestmentTransactionsController extends Controller {
public function __construct() {
}
public function getIndex() {
echo 'Here';
}
public function getTransactionsForOffering($offeringID) {
echo $offeringID;
}
}
Ok so the action and the controller do exit, but when I run: php artisan routes:list I get:
[ReflectionException]
Class App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering does not exist
Well obviously App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering is not a class, how ever: App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController is and getTransactionsForOffering is an action.
Whats going on?
I believe your problem is in the routes.php we can use controllers as follows
Route::get('investment-transactions', 'InvestmentTransactionsController#index');
Route::get('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
By default, our controllers are stored in App/http/controllers folder and laravel know it.
I believe you only need to reference the Class like so:
Route::controller('investment-transactions','InvestmentTransactionsController#Index'); //make sure you create a function for the index
Route::controller('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
Assuming you need to show a view for the route investment-transactions create the following function in your controller:
public function index()
{
return view('name-of-your-view-file');
}

Laravel 5 Model namespace class not found

I am trying to store my models in a custom namespace and directory structure as shown here:
I have:
namespace Modules\Core;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
But I am getting:
FatalErrorException in TestController.php line 8:
Class 'Modules\Core\User' not found
Which is wrong anyway, so I thought it must be 'Modules\Core\Models\User'. I tried this and I still got the same error (just with a different class name).
My model:
namespace Modules\Core;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User Extends Eloquent {
protected $table = 'users';
}
How can I get access to this model in the TestController?
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => 'TestController#index']);
});
If your controller is stored in Modules/Core/Controllers, the namespace should be namespace Modules\Core\Controllers;
And likewise, if the model is stored in Modules/Core/Models, its namespace is namespace Modules\Core\Models;
Then, in the controller, import it before using it:
<?php namespace Modules\Core\Controllers;
use Modules\Core\Models\User;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
I had the same problem as above. In my case I had the following namespace:
namespace Modules\XMLApi;
I got the same error as above. When I changed the namespace to the following:
namespace Modules\XmlApi;
Then run the following command:
composer dump-autoload
Then it worked.
You should edit your routes.php file:
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => '\Modules\Core\TestController#index']);
});
to use full together with namespace

Categories