hello I am trying to organize my classes in the composer-phpunit format, my classes already work interlacing them with require but I would like to have my code by folders and not all the files in the src folder my structure is the following
project structure
I thought that adding two namespaces in the psr-4 attribute of the composer file would work this is the structure of my composer file
{
"name": "ramirogg/p4wsnc",
"repositories": [
{
"type":"vcs",
"url":"https://github.com/r4m1r0gg/PIEZAS_4_WEBSITES_NEW_CONCEPT"
}
],
"description": "p4wsnc library",
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"p4wsncbase\\": "src/clases_base/",
"p4wsnctobase\\": "src/clases_directas_ala_base/"
},
"files": [
"src/helpers.php"
]
},
"require-dev": {
"phpunit/phpunit": "^9.5#dev"
}
}
This is the head class and this is how I defined the namespace
<?php
namespace p4wsncbase;
class Head {
//here is all my code if you need to see it let me know
}
this is the sethead class and this is how i call the head class
<?php
namespace p4wsnctobase;
use p4wsncbase\Head;
class SetHead {}
in the same way I do it for the other classes I have an even class for each class within the classes_base folder, for example for the head class I have the sethead class for the body class I have the setbody class and for the tag class I have the settag class the classes that start with set are in the folder classes_directas_ala_base
finally this is how I call the classes sethead, setbody and settag in the index
<?php
require __DIR__ . '/vendor/autoload.php';
use p4wsnctobase\SetHead;
use p4wsnctobase\SetBody;
use p4wsnctobase\SetTag;
I would like to thank you forever, I really would like to maintain this structure, thank you very much in advance
Related
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
I have a folder structure like this:
Root-Folder/backend/user/class-user.php
Root-Folder/backend/profile/class-profile.php
Root-Folder/backend/login.php
Sample class-user.php content:
namespace myapp\user;
class User{
....
}
In my composer.json:
...
"autoload": {
"psr-4": {
"myapp\\": "backend/"
}
},
...
But this way the files are not included because they have a class- prefix in the file name. If I change the file names to user.php and profile.php, they work.
How can I configure composer to include class-user.php too?
PSR-4 requires classes names to be the same as file name that contains it.
In this case you need to use "classmap" directive instead of PSR-4
https://getcomposer.org/doc/04-schema.md#classmap
{
"autoload": {
"classmap": ["backend"]
}
}
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;
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.
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.