Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Problem z załadowaniem klasy za pomocą composera
trifek
post 13.09.2018, 20:11:13
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


Witajcie,
Mam taki układ katalogów:
- moja aplikacja
-- apps
--- Backend
--- Core
---- Core\Config
---- Core\Drivers (Db.php)
---- Core\Main
--- Frontend

Mój plik composer.json:

  1.  
  2. {
  3. "autoload": {
  4. "psr-4": {
  5. "Core\\": "apps/Core/",
  6. "Web\\": "apps/Frontend",
  7. "Cms\\": "apps/Backend"
  8. }
  9. },
  10. "require": {
  11. "php": ">=7.0",
  12. "phpmailer/phpmailer": "~6.0",
  13. "monolog/monolog": "~1.23",
  14. "mpdf/mpdf": "~7.0",
  15. "twig/twig": "~2.5"
  16. },
  17. "config": {
  18. "vendor-dir": "apps/vendor"
  19. }
  20. }
  21.  


W momencie gdy chcę wywołać w pliku moja aplikacja\secret\index.php kod:
  1. require_once ("../apps/vendor/autoload.php");
  2. use Core\Drivers;
  3. use Core\Main;
  4. $bl = new Core\Drivers\Db();
  5.  
  6.  


Plik Db.php wygląda następująco:

  1. namespace Core\Drivers;
  2.  
  3.  
  4. class Db
  5. {
  6.  
  7. ...
  8.  
  9. }
  10.  


otrzymuję błąd:
Fatal error: Uncaught Error: Class 'Core\Drivers\Db' not found in

Dlaczego?
Go to the top of the page
+Quote Post
nospor
post 14.09.2018, 08:32:41
Post #2





Grupa: Moderatorzy
Postów: 36 447
Pomógł: 6292
Dołączył: 27.12.2004




A odpaliles
composer dump-autoload
?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
trifek
post 14.09.2018, 09:21:48
Post #3





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


tak. Korzystam z phpstorm i klikam tam install/update. Wykonałem też polecenie z konsoli i nie pomogło.

Nie wie. czy to ma znaczenie, ale vendor mam tutaj: apps/vendor (composer, mpdf, autoload.php etc).
Pliki composera: composer.json, composer.phar, composer.lock są w głównym katalogu aplikacji
Go to the top of the page
+Quote Post
trzczy
post 14.09.2018, 10:44:25
Post #4





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

Ostrzeżenie: (0%)
-----


  1. require_once ("../apps/vendor/autoload.php");

ta ścieżka jest prawidłowa?

Jak włączysz obsługę błędów, to w przypadku nieprawidłowej ścieżki pokaże się błąd.
Go to the top of the page
+Quote Post
nospor
post 14.09.2018, 11:12:16
Post #5





Grupa: Moderatorzy
Postów: 36 447
Pomógł: 6292
Dołączył: 27.12.2004




@trzczy jakby require nie znalazlo pliku, to bysmy nie widzieli bledu
Fatal error: Uncaught Error: Class 'Core\Drivers\Db' not found in

tylko FATAL o braku pliku


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
trifek
post 14.09.2018, 11:22:46
Post #6





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


dokładnie, ścieżka w required jest poprawna smile.gif
Go to the top of the page
+Quote Post
trzczy
post 14.09.2018, 12:28:06
Post #7





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

Ostrzeżenie: (0%)
-----


  1. ...
  2. "autoload": {
  3. "psr-4": {
  4. "Core\\": "apps/Core/Drivers/",
  5. "Core\\": "../apps/Core/",
  6. "Core\\": "../apps/Core/Drivers/",
  7. "Web\\": "apps/Frontend",
  8. "Cms\\": "apps/Backend"
  9. }
  10. },
  11. ...
Go to the top of the page
+Quote Post
viking
post 14.09.2018, 16:10:33
Post #8





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


A tak na marginesie. Tutaj akurat pewnie jest zła ścieżka podana. Natomiast jak robisz:
Kod
use Core\Drivers;


To nie:
Kod
$bl = new Core\Drivers\Db();


Tylko:
Kod
$bl = new Drivers\Db();


Gdybyś NS użył to już miałbyś błąd wyszukiwania klasy.


--------------------
Go to the top of the page
+Quote Post
Pyton_000
post 15.09.2018, 06:54:56
Post #9





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Albo po prostu:
Kod
$bl = new \Core\Drivers\Db();


Albo
Kod
use Core\Drivers\Db;
$bl = new Db();
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 21:39