Symfony not finding a custom class - php

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

Related

Laravel not finding the controller located in other folder

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;

Problem autoloading class PHP Slim framwork

I'm facing a problem with my slim(http://www.slimframework.com/) application. When I'm trying to load my Database class using namespacing I get this error:
Message: Class 'Craft\Code\CraftDB\Database' not found
I have Database.php in folder app/config/Database.php
My Database class start like this
namespace Craft\Code\CraftDB;
class Database {
I'm trying to use it like this in another file:
use Craft\Code\CraftDB as DB;
class MyOtherClass {
protected $connectDb;
protected $db;
public function __construct() {
$this->connectDb = new DB\Database;
$this->db = $connectDb->connect();
}
My Composer file is :
"autoload": {
"psr-4": {
"Craft\\Code\\": "app/"
}
}
I'm trying to find the problem but I'm lost now. Please help. Thanks
Where you say your file is: app/config/Database.php
Where you tell Composer your file is: app/CraftDB/Database.php
Things simply don't seem to match here. On file system side you have that config level unaccounted for, on namespace side CraftDB level in namespace.
From your description I imagine you need something like:
"autoload": {
"psr-4": {
"Craft\\Code\\CraftDB\\": "app/config/"
}
}

How to add custom class in laravel 5.4.10

I am new in laravel.
I am writing following code in the controller method.
include_once(app_path() .'/Classes/Pgp_2fa.php');
$pgp = new Pgp_2fa();
Following errors are shown.
FatalThrowableError in AuthController.php line 146: Class
'App\Http\Controllers\Pgp_2fa' not found
You have to use the correct namespace for your class.
If you are not using any namespace, you should add a \ in front of the class to let php know that the class exists in the root namespace. Otherwise php will look in the current namespace which is App\Http\Controllers when you are in a controller.
$pgp = new \Pgp_2fa();
This is not just Laravel behavior but php in general.
When you add new classes there are couple way to use them. (I mean, load them)
If your classes has unique name and don't use namespace, then you can add backslash \ start of the class.
$class = new \Pgp_2fa;
But if you want to define them namespaces and use with same name so
many times, then you have to add these classes in composer.json. Open your
composer.json and come into "autoload" section and define class
directory in classmap
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Classes", // so all classes in this directory are going to load
],
And don't forget to say composer dump-autoload after these changes and creating new classes
If you don't want to dump your composer when you add a new class,
then you may want add those classes inside of the psr-4 section in composer
"autoload": {
"psr-4": {
"App\\": "app/",
"App\\Classes\\": "app/Classes",
}
},
you need to add namespace for your custom class
or just add slash on your class name. like :
$pgp = new \Pgp_2fa();
You can call inside the constructor like below, and you can use the
$Pgp_2fa variable in remaining function
class controllername extends Controller
{
public function __construct(Route $route,Request $request)
{
$Pgp_2fa = new \Pgp_2fa();
}
}
You can easily add your custom classes by added it to Composer's autoload array. Go to composer.json and add it to:
"autoload": {
"files": [
"app\\Classes\\Pgp_2fa.php"
]
},
Now in your app folder, under classes folder add your Pgp_2fa.php file which will have your class. You can use it anywhere you want, it should be automatically auto-load-ed.
After adding your class write command:
composer dumpautoload to refresh autoload class mapping.
For example, There is our class Common.php in a directory called Mylibs in app directory.
app/mylibs/Common.php
we need to namespace App\Mylibs;
namespace App\Mylibs;
class Common {
public function getSite() {
return 'AmirHome.com';
}
}
Now, you can access this class using the namespace within your classes just like other Laravel classes.
use App\Mylibs\Common
in Controller:
namespace App\Http\Controllers;
use App\Mylibs\Common;
class TestController extends Controller {
protected $common;
public function __construct(Common $common) {
$this->common = $common;
}
public function index() {
echo $this->common->getSite();
}
}

Laravel 4 - Repository Interface not found

Just started working through laracasts and trying to move on from direct eloquent use in the controllers.
I have implemented everything that I need to but hitting this error:
Class tva\Repositories\VehicleRepositoryInterface does not exist
My folder structure is:
app/
tva/
repositories/
VehiclesController:
use tva\Repositories\VehicleRepositoryInterface;
class VehiclesController extends \BaseController {
protected $vehicle;
public function __construct(VehicleRepositoryInterface $vehicle)
{
$this->vehicle = $vehicle;
}
}
In the repositories folder:
VehicleRepository:
namespace tva\Repositories;
class VehicleRepository implements VehicleRepositoryInterface {
}
VehicleRepositoryInterface:
namespace tva\Repositories;
interface VehicleRepositoryInterface {
}
And also updated my composer.json:
"psr-0": {
"tva": "app/"
},
To me, this should work?
Issue solved, instead of using psr-0 I added the directory to the classmap and all issues solved.

PHP use namespace class inside method in a class of another namespace

Is it possible in some way to instantiate a class inside a namespace, in a method in another class inside another namespace? And with the requested class instantiated from a variable?
Example:
Class to be loaded from loader-class:
namespace application/pages;
class loader
{
private function __construct()
{
echo 'Class loaded...';
}
}
Loader-class:
namespace system/loader;
class loader
{
private $vars;
private function __construct($vars)
{
$this->vars = $vars;
}
private function load_class()
{
require(CLASSES . $this->vars['namespace'] . '/' . $this->vars['class'] . ".php");
use $this->vars['namespace'];
return new \$this->vars['namespace']\$this->vars['class']();
}
}
Sorry for the bit confusing formulation, but i couldn't think of better way to ask the question.
Thanks.
This is the general way to load in namespaces:
http://www.php.net/manual/en/function.spl-autoload.php
However there is a more popular way of doing it using Composer.
Download composer.phar and inside your project directory run: php composer.phar init
Following the interactions.
In your project root, create a src directory then add this to your composer.json file which generated: "autoload": { "psr-0": { "": "src/" } }
Your composer.json file should now look like this:
{
"name": "acme/sample",
"authors": [
{
"name": "Person",
"email": "person#example.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-0": { "": "src/" }
},
"require": {
}
}
Run: php composer.phar install, which will generate a vendors directory and an autoload script.
Create your primary load php file and include the autoload.php file inside.
Now, your namespaces inside the src directory and any imported libraries in vendor, will be exposed to your application.
Checkout symfony/symfony-standard to see a full framework example.

Categories