I'm developing a personal finance application with ZF2 in ZendStudio 11. I'm getting the following fatal error after adding a new controller from New Zend Item wizard. Here's a screenshot.
Error:
_Fatal error: Cannot redeclare class Application\Controller\OutcomeController in **C:\wamp\www\PersonalFinance\module\Users\src\Users\Controller\OutcomeController.php** on line 33_
OutcomeController class code is as follow:
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
/**
* OutcomeController
*
* #author
*
* #version
*
*/
class OutcomeController extends AbstractActionController
{
/**
* The default action - show the home page
*/
public function indexAction()
{
// TODO Auto-generated OutcomeController::indexAction() default action
return new ViewModel();
}
public function fooAction()
{
// This shows the :controller and :action parameters in default route
// are working when you browse to /income/income/foo
return array();
}
}
I've also added the below line to the autoload_classmap.php file:
'Users\Controller\OutcomeController' => __DIR__ . '/src/Users/Controller/OutcomeController.php',
So:
I've added the controller using the wizard
I've added the needed
controller configs to the module.config.php
I've added the
autoloading config line to the autoload_classmap.php
Any ideas why the error happens?
As the error message suggests, the class Application\Controller\OutcomeController is already defined somewhere else in your application.
This has nothing to do with using the wizard of ZendStudio, configuring correctly ZF2 or with autoloading. This is a PHP thing: you can't have in your application two classes with the same name (considering the namespace).
To avoid the problem you could either change the name of the class or change the namespace where you declare it.
Related
I've had to try and work with an older package meant for php into my Laravel project.
I've added two custom classes, both are in the same folder "Classes" under the main "app" folder in my Laravel project.
One of these classes is recognized from a generated controller for my Laravel project. My paymentsController has use App\Classes\Quickbooks_Payments; in the top.
However, going to that Classes' file, I hit the following error through a route leading to my controller:
Class 'App\Classes\Quickbooks_Loader' not found
Now this is where this above is referenced in my paymentsController file:
<?php
namespace App\Classes\Quickbooks_Payments;
use App\Classes\Quickbooks_Loader;
/**
* QuickBooks Payments class
*
*/
/**
* Utilities class (for masking and some other misc things)
*/
QuickBooks_Loader::load('/QuickBooks/Utilities.php');
This last line is where the above error is referenced. And I do have both the Quickbooks_Loader.php and the Quickbooks_Payments.php in the same folder. My Quickbooks_Loader.php file starts off as such:
<?php
namespace App\Classes\QuickBooks_Loader;
So I know this is likely because of my inexperience with custom/imported classes. What am I doing wrong and how should I properly "import" these custom classes and have them recognized without any issues?
Change namespace in both classes you've shown to:
namespace App\Classes;
And run composer du
Also, make sure the class looks like this and not like you've shown (I'm not sure about did you cut something or not):
<?php
namespace App\Classes;
use App\Classes\Quickbooks_Loader;
class Quickbooks_Payments
{
public function __construct()
{
dd('It\'s working!');
}
}
I created class named Task in the App\Entity namespace and my problem is in my another class I want to use it but it doesn't detect my class and give me error:
Attempted to load class "Task" from namespace "App\Entity".
Did you forget a "use" statement for another namespace?
this my Task.php:
namespace App\Entity;
class Task
{
protected $task;
public function getTask()
{
return $this->task;
}
public function setTask($task)
{
$this->task = $task;
}
}
This is my DefaultController that uses the Task class:
use App\Entity\Task;
class DefaultController
{
}
Depending upon your autoloader settings, if you are using composer, you can have it such that autoloading only happens from the pre-built class map - called 'authoritative classmap' (https://getcomposer.org/doc/articles/autoloader-optimization.md).
So, when you add a class to your source, it isn't recognised by the pre-built autoloader. Try running composer dump to let the autoloader load via standard PSR-0/PSR-4 rules.
If you are using the autoloader on PSR-0/PSR-4 mode, then maybe you've not put the file in the right directory.
I am working in laravel 5.5. I have created a controller folder with name Abc and inside it created a controller file with name abc.php, inside this file I have written code:
namespace App\Http\Controllers\Abc;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\DB;
class abcController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function NewPromotion()
{
return view('new-promotion');
}
}
And in Route folder under web.php file I am calling its view file, like:-
Route::get('/new-promotion','Abc\abcController#NewPromotion');
In spite of defining in namespace I am still getting error i.e.
"ReflectionException (-1)
Class App\Http\Controllers\Abc\abcController does not exist".
What could be the possible issue?
You should make sure your file is located in app/Http/Controllers/Abc directory and has name abcController.php (case sensitive).
Also by convention class names start with big letter, so you should rather name your controller AbcController and have this file with name AbcController.php (and also update your route file to use valid case of this controller name).
Good way to create controller is use cmd (you have to be in project directory)
php artisan make:controller "Abc\abcController"
I am trying to inject a Manager class into toe Service Container of Lumen. My goal is to have a single instance of LogManager which is available in the whole application via app(LogManager::class).
Everytime i try to access this shortcut i get the following exeption:
[2017-03-23 16:42:51] lumen.ERROR: ReflectionException: Class LogManager does not
exist in /vendor/illuminate/container/Container.php:681
LogManager.php (i placed that class in the same location where my models are (app/LogManager.php))
<?php
namespace App;
use App\LogEntry;
class LogManager
{
...
}
AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\LogManager;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->singleton(LogManager::class, function ($app) {
return new LogManager();
});
}
}
I uncommented the line $app->register(App\Providers\AppServiceProvider::class); in bootstrap/app.php
I think that i missed something with the correct namespacing or placement of the classes espaccially LogManager. Maybe some one is willing to give me a hint?
If you need some more informations just give me a hint!
Your class and your service provider look fine. However, wherever you're calling app(LogManager::class) also needs to know the fully qualified name of the class.
Either make sure you have use App\LogManager at the top of the file, or change your call to app(\App\LogManager::class).
OK, so I am trying to create a working Facade for the Resizer bundle for Laravel 3 (https://github.com/maikeldaloo/Resizer).
So far I have:
Created a "Resizer.php" file with the code from the Reszier bundle and added the namespace BARThompson\Planesaleing;
Created a "ResizerFacade.php" the contents of which are contained at the bottom of this post;
Created a "ResizerServiceProvider.php", the contents of which are contained at the bottom of this post;
These three files are all saved in: app\library\;
Added an autoloader to \app\config\app.php under the 'providers' => array which reads:
'BARThompson\Planesaleing\ResizerServiceProvider',
Added an alias to \app\config\app.php under the 'aliases' => array which reads:
'Resizer' => 'BARThompson\Planesaleing\Resizer',
Added the following directory to the autoload classmap in composer.json:
"app/library"
However, I can load the website successfully, but when I call Resizer::open, I get the following error:
Class 'BARThompson\Planesaleing\Config' not found
I am getting confused with namespaces. I havent used them anywhere else in my app, but as I followed a tutorial which used them (http://fideloper.com/create-facade-laravel-4) I have used them in my implementation without fulling understanding them.
Can anyone explain where I am going wrong?
ResizerFacade.php:
<?php
namespace BARThompson\Planesaleing\Facades;
use Illuminate\Support\Facades\Facade;
class Resizer extends Facade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() { return 'resizer'; }
}
ResizerServiceProvicer.php:
<?php
namespace BARThompson\Planesaleing;
use Illuminate\Support\ServiceProvider;
class ResizerServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
// Register 'resizer' instance container to our UnderlyingClass object
$this->app['resizer'] = $this->app->share(function($app)
{
return new BARThompson\Planesaleing\Resizer;
});
}
}