I am facing one problem I do not know what is a reason of this I add class with location in my project src/ApiMaps/ApiMapBundle with this name space
<?php
namespace ApiMaps\ApiMapService;
class ApiMapService {
private $transport;
public function __construct() {
$this->transport = 'sendmail';
}
// ...
}
when i give in
src/config/service.yml
app.test:
class: ApiMaps\ApiMapService\ApiMapService
arguments: ["#doctrine.orm.entity_manager"]
and when i run it from some other class for example
src/ApiMaps/ApiMapBundle/Command/GetApiCommand.php
class GetApiCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$number = $this->getContainer()->get('app.test');
}
}
it give me error
Fatal error: Class 'ApiMaps\ApiMapService\ApiMapService' not found in D:\xampp\htdocs\ProjectMapApiData\a
pp\cache\dev\appDevDebugProjectContainer.php on line 325
[2016-02-01 08:25:20] php.CRITICAL: Fatal Error: Class 'ApiMaps\ApiMapService\ApiMapService' not found {"
type":1,"file":"D:\xampp\htdocs\ProjectMapApiData\app\cache\dev\appDevDebugProjectContainer.php","
line":325,"level":-1,"stack":[]}
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "ApiMapService" from namespace "ApiMaps\ApiMapService".
Did you forget a "use" statement for another namespace?
Note--
one thing to mention that when i try to make service from the built-in class of symfony2 classes it does not give me such error. I do know where I need to add the namespace of the class which i recently added with my project that it able to know the class...
You mention that your service in file:
src/ApiMaps/ApiMapBundle
but in config you wrote:
ApiMaps\ApiMapService\ApiMapService.
I think you should house you service in file: src/ApiMaps/ApiMapBundle/ApiMapService/ApiMapService.php,
take namespace: ApiMaps\ApiMapBundle\ApiMapService
and write in config: ApiMaps\ApiMapBundle\ApiMapService\ApiMapService.
Truly believe it'll help you...
Related
Ok, so I have a project my mongo connection works fine when viewing pages etc. But when I try to load the manage class in command (ContainerAwareCommand) I get class not found exception:
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "Manager" from namespace "MongoDB\Driver".
Did you forget a "use" statement for another namespace?
I'm using symfony 3.2
Here's what I do:
class TestCommand extends ContainerAwareCommand {
protected function configure() {
$this->setName('testcommand:test')
->setDescription('Test...')
->setHelp('Test...');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$db = new Manager("mongodb://localhost:27017");
$output->writeln([
'Test',
'========================',
'',
]);
}
}
I import the driver with:
use MongoDB\Driver\Manager;
PhpStorm recognizes the import.
Any ideas what stops the class from loading? Once again, it loads normally when I navigate my pages etc.
Thanks!
UPDATE (As requested) !
Registered a service:
services:
test.service:
class: AppBundle\Test\TestService
public: true
The service:
namespace AppBundle\Test;
use MongoDB\Driver\Manager;
class TestService {
public function foo() {
$manager = new Manager("mongodb://localhost:27017");
}
}
added this to execute method:
$ts = $this->getContainer()->get("test.service");
$ts->foo();
result:
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "Manager" from namespace "MongoDB\Driver".
Did you forget a "use" statement for another namespace?
What am I missing?
ANOTHER UPDATE
I also noticed that I couldn't switch to production because of the same, but more general, error.
Turns out I can't use namespaces but can get the class (Manager) via fully qualified name at RUN TIME ONLY!!!
I had had the same problem and was somewhat distracted by the error message, this problem occurs while executing the script in the CLI context where not all extension has been properly installed.
Check whether all your extension, escpecially for MongoDb are installed.
I try extend Validator class. I need add a few methods, that I'd like extend all class not use Validator::extend();
I added in vendor direcotry structure:
-comjaroapp
-src
-Comjaroapp
-Validation
-Validator.php
-ValidatorServiceProvider.php
In my config/app.php in providers array, I added:
'Comjaroapp\Validation\ValidatorServiceProvider'
Code to test is simple:
Validator:
namespace Comjaroapp\Validation;
class CustomValidator extends \Illuminate\Validation\Validator{
public function validatePesel($attribute,$value,$options=null){
return true;
}
}
ValidatorServiceProvider:
namespace Comjarospp\Validation;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider{
public function register(){}
public function boot(){
$this->app->validator->resolver(function($transator,$data,$rules,$messages){
return new CustomValidator($transator,$data,$rules,$messages);
});
}
}
After run composer update I see error:
> Error Output: PHP Fatal error: Class
> 'Comjaroapp\Validation\ValidatorServiceProvider' not found in
> /vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php
> on line 158
When I looked on other extension all work with same structure.
If someone have idea, what is wrong or when should I search, please help.
Thanks in advance.
You have different names for your namespaces:
Comjarospp\Validation
and
Comjaroapp\Validation
EDIT:
After fixing the namespace name, have you executed
composer dumpautoload
?
I use symfony 2.4.0. I want to use my custom class as discussed here: Autoloading a class in Symfony 2.1. I have created subfolder in src:
namespace Yur;
class MyClass {
public go() {
var_dump('hello!! 32');
}
}
In my controller, I made this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Yur\MyClass;
class WelcomeController extends Controller
{
public function indexAction()
{
$my = new MyClass();
$my->go();
die();
...
but it makes an exception:
ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?
After I have got this exception, I decided consciously to make syntax error exception in my class to see if it loaded. I changed class MyClass ... to class4 MyClass ..., but doesnt got asyntax error` exception. And I decided, that my class is not loaded.
Is anyone knows why? And what I must to do to resolve?
A few things. First, in your code sample above, you have
public go() {
var_dump('hello!! 32');
}
which should be
public function go() {
var_dump('hello!! 32');
}
The former raises a parser error in PHP. and probably isn't what you want.
Second, the error
ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?
is the error Symfony uses when it attempt to autoload a class, but can't find the file. This usually means your file is named incorrectly, or in the wrong folder. I'd tripped check that you have a file in the directory you think you do.
$ ls src/Yur/MyClass.php
You can also add some debugging to the composer autoload code to see what path it's cooking up for your custom class
#File: vendor/composer/ClassLoader.php
public function findFile($class)
{
//...
$classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php';
//start your debugging code
if($class == 'Yur\MyClass')
{
//dump the generated path
var_dump($classPath);
//dump the default include paths
var_dump($this->fallbackDirs);
}
//...
}
I'm having a problem setting up a service in Symfony. I get the following error when I run the app:
The autoloader expected class "HRPortal\SystemBundle\Services\SessionHandler" to be
defined in file "/usr/local/apache2/htdocs/hrportal/src/HRPortal/SystemBundle/Services/SessionHandler.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.
The class is the following:
<?php
namespace HRPortal\SystemBundle\Services\SessionHandler;
class SessionHandler
{
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function init($user)
{
...
}
....
}
And the declaration of my service is as follow:
parameters:
session_handler.class: HRPortal\SystemBundle\Services\SessionHandler
services:
session_handler:
class: "%session_handler.class%"
I have tried many things that did not work, so I thought I'd ask the community and see what you can come out with.
Thank you
Namespace should be
namespace HRPortal\SystemBundle\Services;
That way the fully qualified class name would be HRPortal\SystemBundle\Services\SessionHandler
This is my first question, besides I'm not english-native speaker, so sorry in advance for newbie mistakes...
I'm starting with Symfony2, and I've been facing an autoload problem for a couple of days, i'm getting crazy..
I'm just trying to use a PHP class inside my DefaultController of my AppBundle. I've read the way of doing this is by creating a service in my config.yml and giving a namespace to that class that matches.
Symfony tells me that it does found the file but the class is not in it, the exact error is:
The autoloader expected class "Priceget\CollectorBundle\Crawler\Amazon" to be defined in file "/srv/www/lol.com/public_html/priceget/symfony/src/Priceget/CollectorBundle/Crawler/Amazon.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
And my class is just this:
<?php
namespace Priceget\CollectorBundle\Crawler\Amazon;
use Symfony\Component\HttpFoundation\Response;
class Amazon
{
public function getAll()
{
return new Response('l0l');
}
}
In my DefaultController I'm calling it like that:
<?php
namespace Priceget\CollectorBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Guzzle\Http\Client;
use Symfony\Component\DomCrawler\Crawler;
use Priceget\CollectorBundle\Crawler\Amazon;
class DefaultController extends Controller
{
public function indexAction()
{
$amazon = $this->get('amazon.crawler');
}
}
And my config.yml piece:
services:
amazon.crawler:
class: Priceget\CollectorBundle\Crawler\Amazon
I've already tried to:
Empty cache
Restart apache
Extend the class to Controller? :-Z
Thank you so much in advance.
Your namespace is wrong, rename it:
from: namespace Priceget\CollectorBundle\Crawler\Amazon;
to: namespace Priceget\CollectorBundle\Crawler;
This error also occurs if you do not put <?php in the beginning of the file.
In addition to what's said by Igor, you obviously have to change the FQN class name in the service declaration (YML) if you want it to work.
This can be a bit misleading, it also happens if you don't extend your class correctly. In my instance I tried to extend a repository with an incorrect FQN:
class FilesRepository extends Doctrine\ORM\EntityRepository
should have been:
class FilesRepository extends \Doctrine\ORM\EntityRepository
Notice the missing backslash (\).