Laravel not finding the controller located in other folder - php

I'm trying to implement a clean architecture in laravel, thus I'm moving my own code to a src folder.
My controller is located in src\notebook\infrastructure but when i call it from routes\web.php this way:
Route::get('/notebook', 'src\notebook\infrastructure\NotebooksController#show');
i got this error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [src\notebook\infrastructure\NotebooksController] does not exist.
http://127.0.0.1:8000/notebook
i also changed the namespace value in the class RouteServiceProvider from:
protected $namespace = 'App\Http\Controllers';
to
protected $namespace = '';
This is my notebook controller class:
namespace src\notebook\infrastructure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NotebooksController extends Controller
{
public function show($id)
{
echo 'controller from infrastructure folder';
}
}
My laravel and php version in composer.json are:
"php": "^7.2.5",
"laravel/framework": "^7.24",
I feel like i'm missing something stupid but can't figure it out what.

Did you add src folder into the autoload?
In composer.json file, you must have something like this:
"autoload": {
"psr-4": {
"App\\": "app/",
"Src\\": "src/" // add this
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After changing it run composer dump-autoload.
And also don't forget to follow psr-4 rules and use Studly case namespace and class names.
namespace Src\Notebook\Infrastructure;

Related

Custom helper not working in laravel, it return undefined function in controller

Edit For those who may bump into this thread with similar error
Remove the namespace from the helpers.php file as it is not a class.
In my own case, the correct code should be
use Illuminate\Http\Request;
use App\Models\User;
use Session;
function showTest(){
return "test";
}
..and not
namespace App\Helpers;
use Illuminate\Http\Request;
use App\Models\User;
use Session;
function showTest(){
return "test";
}
Original Question
I am looking to write a global function in laravel using the helper.
Here is my code
helpers.php
namespace App\Helpers;
use Illuminate\Http\Request;
use App\Models\User;
use Session;
function showTest(){
return "test";
}
Composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files":[
"app/Helpers/helpers.php"
]
},
But when i call the function in the controller it returns error;
dd(showTest());
it returns this error;
Call to undefined function App\Http\Controllers\User\showTest()
What could be the problem?
Also I have ran composer dump-autoload
Modified
I have tried all solutions posted here but it’s not working.
Php 8
Laravel 9
You need to run composer dump-autoload and it will work.
For more details click here
Another way of doing this is to register your Helper files in Laravel's service container.
You could either do this in an existing service provider file, or create a new one, for example HelperServiceProvider.php (can be done with php artisan make:provider)
In the register method of the service provider, you can take your helper file and require them globally, as such
public function register()
{
require_once(glob(app_path().'/Helpers/helpers.php');
}
If you have multiple files, you can simply put them in a for each
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
Try loading them inside AppServiceProvider boot method instead of composer file.
require_once app_path('Helpers/helpers.php');
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files":[
"app/Helpers/helpers.php",
"app\\Helpers\\helpers.php",
]
},
hepler.php
<?php
if(!function_exists('showTest')){
function showTest()
{
return "test";
}
}
if(!function_exists('showTest2')){
function showTest2()
{
return "test 2";
}
}
And run composer dump-autoload
First I want to apologize for all the troubles.
The problem was that I used namespace in my helpers.php file
This thread saved me
https://stackoverflow.com/a/29736097/14940381

App/Controller not found in REST API Project

This is pure php project
Here is my composer.json code , when i debug "CatalogController.php" file , it gives me following error . Please help me to resolve this incident
https://prnt.sc/26jxg3y
Folder Structure
https://prnt.sc/26jxgyt
CatalogController.php - https://prnt.sc/26jxjw9
<?php
namespace App;
use Exception;
use App\Controller;
use App\CatalogModel;
use App\JwtMiddleware;
use App\RequestMiddleware;
class CatalogController extends Controller {
......
}
composer.json
{
"require": {
"klein/klein": "^2.1",
"firebase/php-jwt": "^5.2"
},
"autoload": {
"psr-4": {
"App\": "App/"
},
"classmap": [
"App/Controller",
"App/Middleware",
"App/Model"
]
}
}
[+Updated]
Thanks my team. I resolved 90% . But i got this error.
https://prnt.sc/26jzpxp
Please help me to resolve this incident.
invalid namespace App or directory structure, read PSR-4 manualto understand how it use
redundant import use App\*; in CatalogController.php - this classes in App namespace too
invalid JSON "App\"
redundant classmap section in composer.json - read Composer manual to understand how it use

How to keep seeder files separate and clean in laravel?

I want to keep my seeder files separate. for example UsersTableSeeder.php , PostsTableSeeder.php and then call them in main seeder file (DatabaseSeeder.php) :
Example:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(PostsTableSeeder::class);
}
}
usersTableSeeder.php :
<?php namespace App\Seeds;
use Illuminate\Database\Seeder;
use App\User;
class UserTableSeeder extends Seeder {
public function run()
{
//DB::table('users')->delete();
// user1
User::create(array(
'name' => 'ahmad',
'email' => 'ahmad#ahmad.com',
'password' => 'ahmad'
));
}
}
my UsersTableSeeder.php and PostsTableSeeder.php files are in the same
directory that DatabaseSeeder.php is.
should I use psr-4 autoloading ? how?
Composer.json configuration
composer.json has a autoload key to configure additional autoload paths.
You can give it a try:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
}
Example from my composer.json
Remove the namespace line from your seeder classes. They are found by the classmap entries now.
Explicit namespaces
OR
Keep the namespaces and add them in calls to ->call().
Wherever you write a classname you can fully qualify the class with its namespace (should be \database\seeds\YourClass::class, which can depend on your composer.json settings), or add a use statement at the beginning of the file.
You may have to run composer dump-autoload. It worked for me.
As a side note, it'd be cleaner to use PSR-4 for seeds and migrations (tests already do).

Laravel and using namespaces - Class not found

I have created a php file under the Models folder provided by Laravel.
Inside I have:
// Filename: Models/Shoopy.php
namespace Whatever\Something;
class Shoopy
{
public function Toot(){};
}
Now in another Model file I am using that namespace:
use \Whatever\Something\Shoopy;
class AnotherModel
{
public function Spop()
{
$harr = new Shoopy();
}
}
It comes up with a Laravel error:
class Whatever\Something\Shoopy not found
Any ideas?
I know this is an older issue, but if you go into your composer.json file, you'll probably see something like...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
What you can do is tell composer where Whatever should be, say if it was a different location (or sub-folder) from where Laravel normally keeps it's files.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Whatever\\": "app/Whatever",
}
},
Classes need to exist in files with directory paths that match their namespace. This is according to the PSR-0 Standard. In your example, try placing your Shoopy class in the file:
app/models/Whatever/Something/Shoopy.php
And then see if the autoloader can find it.

Symfony not finding a custom class

In my folder "/Vendor/User/Admin" I created a new custom class (Adminuser.php)
namespace \User\Admin;
class Adminuser {
public $username;
public $password;
}
Now Im trying to use it in a controller:
namespace Section\AdminBundle\Controller;
use \User\Admin;
class DefaultController extends Controller
{
public function indexAction()
{
$AdminUser = new \User\Admin\Adminuser(); // CLASS NOT FOUND!!
.......
Why is this happening?, the namespace is wrong? (I tried a few options..)
Im very begginer with Symfony, sorry.
You have 2 main issues.
The First
When declaring a namespace you should not start with a \
namespace \User\Admin;
Should just be:
namespace User\Admin;
The Second
If you want those classes to live in your Vendors Dir then you need to make sure the class is being autoloaded by symfony correctly. To do this we will use composer.
In your composer.json you will want to change this section from:
"autoload": {
"psr-0": { "": "src/" }
},
TO:
"autoload": {
"psr-0": { "": "src/" },
"psr-0": { "": "vendor/User/Admin" }
},
Then composer will add classes under that folder to the available namespaces and you will be able to access it as expected.
just remove the first "\" in the namespace, as the comments said. So the first file is:
namespace User\Admin;
class Adminuser {
public $username;
public $password;
}
if the problem persist check your autoloading configuration, maybe the right way would be using src dir to develop your code, not vendor :S

Categories