Problem autoloading class PHP Slim framwork - php

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/"
}
}

Related

Class Not Found in PHP Laravel 6.x when another class extends

I always get the class not found exception in php laravel 6 when i create a class and extend a parent class named A which is located in the same directory.
However, another child class that is located in same directory could extend class A successfully.
In addition, i couldn't also instantiate the class A due to class not found exception in another .php file.
Please help me on this.
Thanks in an advance.
Parent class: myContext
<?php
namespace config\models;
class myContext {
public static $conn;
...
}
Class myUser: extension is fine.
<?php
namespace config\models;
class myUser extends myContext {
private $name;
...
}
Class oauth: extension returns myContext class not found.
<?php
namespace config\models;
class oauth extends myContext {
private $user;
}
Instantiate the class - returns class not found.
<?php
use config\models\myContext as context;
$cont = new context();
Check whether the namespace is added correctly when the parent class is imported.
Refer
https://www.quora.com/What-is-namespaces-and-use-in-Laravel
external classes and PHP files in Laravel - Class not found
Laravel - Model Class not found
Laravel 6: Class 'Form' not found
To get the provided example codes to work you need to use require_once
<?php
require_once('models/myContext.php');
use app\config\models\myContext as context;
$test = new context();
A search on SO brought me to this source
You try to add this line of code in the composer.json file, then execute the composer dumpautoload command on the command line
In composer.json file,
"autoload": {
"psr-4": {
"App\\": "app/",
"config\\models\\": "config/models"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After that composer dump-autoload.
have you add autoload for config/ folder in composer.json. Laravel only default autoload for app/

Laravel use external class

I'm new on laravel and need some help.
Actualy i use 5.3 laravel version.
I need to use, in laravel, external company framework (call it F for semplicity).
This framework is some like process manager. Actualy, for integrate it with laravel i put it in laravel/public folder, this way i can use laravel and F's pages in same domain. In laravel i need to access to some F functionality wich is all collect in class FFramework\Facade\FInterface;
Then i want to acces to this class from laravel controller FController.
But when i run code i get Class Not Found Exception.
This is folder structure:
laravel
app
http
controllers
FController.php
public
css
js
index.php
fframework
facade
FInterface.php
index.php
This is code example:
FController.php
<?php
namespace App\Http\Controllers;
use FFramework\facade\FInterface;
class FController extends Controller {
public function getSimpleDAta() {
$f = new FInterface();
return $f->getSimpleData();
}
}
FInterface.php
<?php
namespace FFramework\facade;
use some\internal\function;
class FInterface {
public function getSimpleDAta() {
$simpleData = ...omg so much computation :) ...
return $simpleData;
}
}
Exception that i get:
Class 'FFramework\facade\FInterface' not found
Additional Information
I use standard LAMP configuration
PHP version is last 5.6 stable version
Question
How can i use FInterface from Laravel controller leaving FFramework in public folder?
You need to update the autoload section of your composer.json file to tell your project where to look for files in the FFramework namespace.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
"FFramework\\": "public/fframework"
}
},

Custom class in laravel is not being recognized

So I've been trying to create a custom class file for all my custom classes and append them in there, I'm using namespaces in order to accomplish using them in my controller however the controller is not recognizing the class
Ok:
Here is my custom class file placed in my project as app/Library/robloxClasses.php
namespace App\Library;
class RobloxMaths{
public function sortArrayOfArray(&$array, $subfield)
{
$sortarray = array();
foreach ($array as $key => $row)
{
$sortarray[$key] = $row[$subfield];
}
array_multisort($sortarray, SORT_DESC, $array);
}
}
Library is a folder i made to store all my custom class files in there
Here is how my controller ApiController uses the file using it's namespace
<?php
namespace App\Http\Controllers;
use App\Library\RobloxMaths;
use Illuminate\Http\Request;
class ApiController extends Controller
{
protected $RobloxClass;
public function __construct(){
$this->$RobloxClass= new RobloxMaths;
}
}
?>
The problem is, it keeps returning:
Class 'App\Library\RobloxMaths' not found
And I honestly do not know what's wrong, please help!
NOTE: I have tried composer update, and it fixes it temporarily but after a day or two or after me installing new dependencies it notifies me that the Class 'App\Library\RobloxMaths' not found thing happened
help please
Ensure that you have correctly setup the psr-4 autoload root in your composer.json file and that your file/directory names match perfectly including capitalisation, then run composer dump-autoload.
Example:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
This will assign the root namespace App to the app folder.
If you are using an IDE, there is a large chance that your use and namespace statements where auto-generated with the lowercase app to match the directory name. As all the names tie up correctly in the IDE there are no errors, but when it comes to running the PHP script the autoloader is not doing require|require_once on your files and therefore your classes are not available.
Option 1 (Do this)
Ensure that your namespaces have consistent capitalisation that matches the composer.json entry.
Option 2 (Written for completion)
Ensure that you add a new entry for this root namespace to your composer.json file. For the example above this would be:
"autoload": {
"psr-4": {
"App\\": "app/",
"app\\": "app/"
}
}
It's an old question, but I had the same problem with laravel 9,
The problem is that I've used php short tag <? instead of <?php at the custom class file.
it's silly but this was the solution for me.

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.

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