Symfony - Unable to load a listener service - symfony3.x

I'm trying to load a listener but symfony keeps throwing this error:
ClassNotFoundException in appDevDebugProjectContainer.php line 1783:
Attempted to load class "CommandeListener" from namespace "Louvre\ReversationBundle\Services\Listeners".
Did you forget a "use" statement for another namespace?
My class is properly writen (i guess):
<?php
namespace Louvre\ReservationBundle\Services\Listeners;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class CommandeListener {
public function checkCommand(FilterResponseEvent $event){
if (!$event->isMasterRequest() && $event->getRequest()->get('_route') == "louvre_reservation_step1") {
return;
}
$url = $this->router->generate("louvre_reservation_step1");
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
Its path is
src/Louvre/ReservationBundle/Services/Listeners/CommandeListener.php
and here is the yaml service
louvre_reservation.commande.listener:
class: Louvre\ReversationBundle\Services\Listeners\CommandeListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: checkCommande }
I have other services which work perfectly.
they are declared the same way.
I googled the problem but could not find an answer.
Does anyone already faced this problem?
Thanks in advance

It looks like you made a typo in your class include. You wrote "Reversation" instead of "Reservation" which make your code looks into the "Reversation" folder which does not exists.

Related

Why Fatfreeframework (F3) keep give me "HTTP 405 Method Not Allowed" message, even when I have correct Class->method to process the POST|GET request?

I've been trying to solve this strange problem. I just want to redirect a request to do a specific function using POST|GET method.
At first, I thought this was a straightforward scenario, but I found it little bit challenging.
I have this in my routing configuration (for GET method) :
[routes]
GET /pub/adsprocess/#command/#adsid=AdsController->processAds
and the code in my "view" file :
< a href="/pub/adsprocess/yes/1" >YES </a>
< a href="/pub/adsprocess/no/2" >NO </a>
My controller look like this :
class AdsController extends Controller
{
function processAds()
{
$command = $this->f3->get('PARAMS.command');
$ads_id = $this->f3->get('PARAMS.adsid');
/*some more process here...*/
}
}
As far as I see, everything looks ok to me. But, all of this doesn't work, F3 keep showing me "Method Not Allowed HTTP 405" error message. And this error message stay there when I use POST method, too.
I'm not sure where to look. Any clue or alternative approach to this problem will highly appreciated.
Thanks,
Sigit
as i can't comment, can we see the autoloader variable? I mean, if you place the AdsController in a Controller Folder and you didn't add the controller folder to the autoloader, then you need to access it like this:
\Controller\AdsController->processAds
and the AdsController.php file must be added the namespace to works:
Namespace Controller;
class AdsController extends \Controller
{
function processAds()
{
$command = $this->f3->get('PARAMS.command');
$ads_id = $this->f3->get('PARAMS.adsid');
/*some more process here...*/
}
}
if you did add it to the autoloader variable, it's should be work on your way.

I can't use Autoloader from Symfony2 in Symfony 1.4 for load this namespaced classes

I want to use this php library with namespaced classes in my Symfony 1.4 project: https://github.com/donquixote/cellbrush.
I'm not quite familiar with the namespaces concept. So when i fisrt try the to use the main class of this library, according to its docs, i just did:
$table = \Donquixote\Cellbrush\Table\Table::create();
And i got this fatal error:
Fatal error: Class 'Donquixote\Cellbrush\Table\Table' not found in D:\SF_ROOT_DIR\apps\frontend\modules\home\actions\actions.class.php
So i searched for a solution, and supposedly there is one: stackoverflow sol 1, stackoverflow sol 1 eg, but when i try to implement it i still get the above error.
My case:
Directories and files of interest:
D:\SF_ROOT_DIR\lib\autoload\sfClassLoader.class.php
D:\SF_ROOT_DIR\lib\vendor\ClassLoader (contains:
https://github.com/symfony/ClassLoader/tree/2.6)
D:\SF_ROOT_DIR\lib\vendor\cellbrush-1.0 (contains:
https://github.com/donquixote/cellbrush.)
Code:
SF_ROOT_DIR/config/ProjectConfiguration.class.php
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->namespacesClassLoader();
$this->enableAllPluginsExcept('sfPropelPlugin');
}
public function namespacesClassLoader() {
if (extension_loaded('apc')) {
$loader = new ApcUniversalClassLoader('S2A');
} else {
$loader = new UniversalClassLoader();
}
$loader->registerNamespaces(array(
'Donquixote' => __DIR__ . '/../lib/vendor/cellbrush-1.0/src/Table'));
$loader->register();
}
}
actions.class.php
$table = \Donquixote\Cellbrush\Table\Table::create();
Thanks.
Use composer and its autoloading.
Execute:
composer require donquixote/cellbrush
Now the library is installed in vendor directory and autoloader is generated, you just need to include it. Add this line to the top of config/ProjectConfiguration.class.php:
require_once dirname(__FILE__).'/../vendor/autoload.php';

PHP namespace behaviour gives FATAL error with spl_autoload_register

I want to use namespace and spl_autoload_register together but failed with different error each time.
Please See complete code files on github.
Below are the files
a base file where create a class with namespace class.alpha.php
an include file where I define spl_autoload_register include.php
an example file which instantiate the class object eg.php
Now when I create object from eg.php it gives FATAL error but when I comment namespace line in class.alpha.php then it's working
Please see the code below.
alpha.class.php
<?php
//namespace Alpha; //<< comment and uncomment this to regenerate the error
class Alpha
{
// public static $baseDir_;
public $dir = __DIR__;
public static $baseDir_;
public function __construct()
{
echo __FILE__."=>".__METHOD__;
var_dump(self::$baseDir_, $this->dir);
$firstDir = !empty(self::$baseDir_) ? self::$baseDir_ : $this->dir;
}
}
include.php
<?php //namespace Alpha\config;
spl_autoload_extensions(".php");
spl_autoload_register('loadclass');
function loadclass($class)
{
try {
if (is_readable(strtolower($class).".class.php")) {
include_once strtolower($class).".class.php";
}
} catch (Exception $e) {
print "Exception:". $e;
}
}
//#link http://php.net/manual/en/function.spl-autoload-register.php
// spl_autoload_register(__NAMESPACE__.'Alpha\Alpha()' );
eg.php
<?php
require_once 'include.php';
/** below code works by commenting 1st line on alpha.class.php
if we un comment then below code gives Fatal error: Class 'Alpha' not found */
Alpha::$baseDir_ = '/opt/lampp/archive/';
$obj_ = new Alpha();
var_dump(get_included_files());
var_dump($obj_);
/** now we define namespace Alpha on alpha.class.php */
// $ns_ = new Alpha\Alpha(); // Fatal error: Class 'Alpha\Alpha' not found
// var_dump($ns_);
/** not working even with use statement */
// use Alpha;
// use Alpha;
// $fn = new Alpha\Alpha();
// var_dump($fn);
Please help me out to solve this issue.
Thanks
Your autoloader is receiving a request for a class of "Alpha\Alpha" if you uncomment the namespace in alpha.class.php and place the use Alpha\Alpha in eg. This means that the location it's expecting to find your class in would be alpha\alpha.class.php.
Unless you're on Windows, directory separators are typically forward slash (/). So there's a number of possible solutions.
**Possible Solution #1 - Leave all files in the same place **
If you want to leave everything where it is now, you'll need to remove the namespace from the class names in the autoloader. If you add these lines to the top of your autoloader, that will make it behave that way:
$classParts = explode("\\", $class);
$class = $classParts[count($classParts) - 1];
I would not recommend this solution though since it means that you can no longer provide the same class name in a different namespace.
Possible Solution #2 - Put namespaces in subdirectories
For this solution, you'd create a new directory "alpha" and move "alpha.class.php" into it. For autoloader changes, you can add the following lines to the top of your autoloader:
$class = str_replace("\\", "/", $class);
This will change the namespace separators from backslashes to file path separators with forward slash. This will work on windows as well as mac and linux.
Possible Solution #3 - Follow an established autoloading standard
There are already a number of standard PHP autoloading standards. PSR-0 (now deprecated) works, but PSR-4 would be recommended:
PSR-0: http://www.php-fig.org/psr/psr-0/
PSR-4: http://www.php-fig.org/psr/psr-4/
One big upside of following one of these standards is that there are already plenty of implementations for them and there's been a lot of thought put into how they should work and maintain compatibility with other libraries you may end up wanting to use. Composer (http://getcomposer.org) will allow you to set up and use both PSR-0 and PSR-4 style autoloaders based on a very simple configuration.
Anyway, for the TL;DR crowd, the issue is that the autoloader receives the entire namespaced path in order to know how to load the class. The fatal error was because the autoloader wasn't properly mapping from that namespaced class to a file system location, so the file containing the class was never being loaded.
Hope this helps.

Laravel 3: Class not found in namespace

This is the problem: I haven't be able to load a class from a namespace.
I'm developing a restful app and I'm trying to follow the Entity/Service/Repository way to access and give format to the requested data. The thing is that I cannot load any class from Services. I have created a folder inside my app called api, and within it the others 3 folders: api/entities/, api/services/ and api/repositories/. There are 2 more folders inside services: validators and datapickers.
As it is a RESTulf app, I also created an api folder inside the controllers folde: controllers/api/.
Here is the the current tree of my app folder:
app/
api/
entities/
repositories/
services/
datapickers/
MemberData.php
ConsumeData.php
validators/
...
models/
controllers/
api/
members.php
(other restful controllers)
languages/
...
In first instance, this is my Autoload section from start.php:
Autoloader::directories(array(
path('app').'models',
path('app').'libraries',
path('app').'api'
));
Autoloader::namespaces(array(
'Api' => path('app').'api',
));
And this is MemberData.php:
<?php
namespace Api\Services\Datapickers;
use Api\Repositories as Repo;
class MemberData
{
/* Do some stuff */
}
Now, when I try to use MemberData in controllers/api/members.php:
<?php
use Api\Services\Datapickers\MemberData;
class Api_Members_Controller extends Api_Base_Controller
{
public function get_index($id = null)
{
if (!empty($id))
$this->params->data = MemberData::getById($id);
else
{
$this->params->data = MemberData::getAll(
Input::get('offset'),
Input::get('limit')
);
}
return Response::json($this->params, 200);
}
}
I get the following:
Unhandled Exception
Message:
Class 'Api\Services\Datapickers\MemberData' not found
Location:
/path_to_project/application/controllers/api/members.php on line 15
which is this line: $this->params->data = MemberData::getById($id);
I autoload the api folder and register the base api namespace in start.php but I still keep receiving the same error. IT's like Laravel doesn't recognize the api namespace or something like that.
I tried to register the full namespace:
Autoloader::namespaces(array(
'Api\Services\Datapickers' => path('app').'api/services/datapickers',
));
but the error I got was: Call to undefinde method 'Api\Services\Datapickers\MemberData::getById()'. This was just a test (I don't want to register every sub-namespace in the Autoloader).

creating functions in CodeIgniter controllers

I have a CodeIgniter application, but one of my controllers must call a data processing function that I have also written myself. The only problem is I can't seem to figure out how to do this. Looking through the user guide it seems that I should put my function inside the class declaration, and prefix it with an underscore (_) so that it cannot be called via the url. However, this is not working. Here's an example of what I mean:
<?php
class Listing extends Controller
{
function index()
{
$data = "hello";
$outputdata['string'] = _dprocess($data);
$this->load->view('view',$outputdata);
}
function _dprocess($d)
{
$output = "prefix - ".$d." - suffix";
return $output
}
}
?>
The page keeps telling me I have a call to an undefined function _dprocess()
How do I call my own functions?
Thanks!
Mala
Edit:
I've gotten it to work by placing the function outside of the class declaration. Is this the correct way of doing it?
This line is creating problem for you:
$outputdata['string'] = _dprocess($data);
Replace with:
$outputdata['string'] = $this->_dprocess($data);