Trait not found error magento2 - php

I am installing a module, where I am getting the error when
running
php bin/magento setup:di:compile.
The error is like:
Fatal error: Trait 'Niks\LayeredNavigation\Model\Layer\Filter\SliderTrait'
not found in /var/www/clients/client2/web202/web/app/code/NIKS
/LayeredNavigation/Model/Layer/Filter/Decimal.php on line 10
When I check the Decimal.php the code is like
<?php
namespace Niks\LayeredNavigation\Model\Layer\Filter;
use Magento\CatalogSearch\Model\Layer\Filter\Decimal as CoreDecimal;
/**
* Layer attribute filter
*/
class Decimal extends CoreDecimal
{
use SliderTrait; //This is relevant line 10 referred in code
...
and in the file SliderTrait.php it is like:
<?php
namespace Niks\LayeredNavigation\Model\Layer\Filter;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
trait SliderTrait
{
/** #var \Niks\LayeredNavigation\Model\ResourceModel\Fulltext\Collection|null */
protected $_skipFilterCollection;
...
so to me the code looks okay and am not able to find any issue, why this error is generated.

I can see that your vendor folder name is "NIKS", compared to the vendor namespace "Niks". It seems like Magento 2 folder structure is case sensitive inside /app/code for autoloading classes. Either change the namespace to "NIKS", or the folder name to "Niks". That should solve it!

Related

Overriding ControllerTask using plugin

I would like to produce an extra view file, inside the model's template folder when I bake a controller. I've copied (unchanged yet) ControllerTask.php into plugins/WetKit/src/Shell/Task in my plugin.
For instance, for the model Entries, I would like a new file to appear during baking: src/Templates/Entries/my_custom_file.ctp.
However, when I run cake bake controller entries -t WetKit, I get:
$ bin/cake bake controller entries -t WetKit
PHP Fatal error: Cannot declare class Bake\Shell\Task\ControllerTask, because the name is already in use in C:\Users\me\Downloads\xampp3\xampp\htdocs\twofk\plugins\WetKit\src\Shell\Task\ControllerTask.php on line 28
What is the best way to accomplish this?
Edit 1: Would this be a namespace issue?
Edit 2:
Edits made to the WetKitControllerTask.php, kept as-is, but added "Yo!" in the output of the sprintf function:
namespace Wetkit\Bake\Shell\Task;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Bake\Shell\Task\ControllerTask;
/**
* Task class for creating and updating controller files.
*
* #property \Bake\Shell\Task\ModelTask $Model
* #property \Bake\Shell\Task\BakeTemplateTask $BakeTemplate
* #property \Bake\Shell\Task\TestTask $Test
*/
class WetKitControllerTask extends ControllerTask
...
public function bake($controllerName)
{
$this->out("\n" . sprintf('Yo! Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
When I run the bake command, I don't seem to be getting "Yo!" to appear:
$ bin/cake bake controller entries -t WetKit
Baking controller class for Entries...
Edit 3
I get an error:
file: plugins/WetKit/src/Shell/Task/WetKitControllerTask.php
class name: ControllerTask
result: Cannot declare class Wetkit\Bake\Shell\Task\ControllerTask because the name is already in use
I get an error:
file: plugins/WetKit/src/Shell/Task/ControllerTask.php
class name: ControllerTask
result: Cannot declare class Wetkit\Bake\Shell\Task\ControllerTask because the name is already in use
No error, but doesn't output desired line:
file: plugins/WetKit/src/Shell/Task/ControllerTask.php
class name: WetKitControllerTask
result: no issues, but also not displaying added output command
Command
public function bake($controllerName)
{
$this->out("\n" . 'Override damn you!');
Under the Include tab in DebugKit, I see that my WetKit plugin is loaded.
To confirm, the override should happen in plugins/WetKit/src/Shell/Task/ of the plugin?
Edit 4
The namespace issue was my fault. I introduced this while trying to override the task.
In terms of autoloader, in composer.json:
"autoload": {
"psr-4": {
"WetKit\\": "src/"
}
},
With:
Namespace: WetKit\Shell\Task
Filename: ControllerTask.php
Class name: WetKitControllerTask
I decided to refresh the autoload files:
$ composer dump-autoload -o
Generating optimized autoload files
Class WetKit\Shell\Task\WetKitControllerTask located in C:/Users/me/Downloads/xampp3/xampp/htdocs/twofk/plugins/WetKit/src\Shell\Task\ControllerTask.php does not comply with psr-4 autoloading standard. Skipping.
Generated optimized autoload files containing 3261 classes
If I change the filename to match the classname, composer will load the class and not output the PSR-4 issue. The baking still doesn't work, I don't see the class changes.
I tried to bake with the filename being ControllerTask.php and class name WetKitControllerTask. Composer would have skipped the file, but baking still doesn't show my class modifications. Strange.
Edit 5
Noting a change to targeting TemplateTask.php instead of ControllerTask.php. What I need to do fits better in TemplateTask.php.
With the class and filename matching, I cannot bake as I get the error shown below (which also happened with ControllerTask.php):
Edit 6
Solution:
...
use Bake\Shell\Task\TemplateTask as WetKitTemplateTask;
...
class TemplateTask extends WetKitTemplateTask
I just had to change the namespace and class definition as such and place the file under plugin/WetKit/src/Shell/Task.
...
use Bake\Shell\Task\TemplateTask as WetKitTemplateTask;
...
class TemplateTask extends WetKitTemplateTask

Custom class not found in a Laravel app

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!');
}
}

(1/1) FatalErrorException Class 'App\User' not found in HomeController.php (line 28)

I'm actually making a laravel CRUD app.
So there is this model called User which has been created, but when I try to use it in a controller (In this case, HomeController.php), it says:
Here is line 28 from the controller:
I'm sorry if this question already exists but I've searched everywhere for a solution but could not find it.
Thank you.
Ideally, you should post the line you are importing the model, and the first few lines of your model.
But I will try to explore the possibilities of error.
Is your model inside a specific folder (just like app/MyModels/User.php) or just inside the default place when you use php artisan make:model? Remember to put the exact path in namespace line inside of model.
In the first line of your model you should have the path of your model with namespace. If the model is in its default directory, you should have the line below, but if it is inside of a folder, you have to put the folder path after de App:
namespace App;
Verify if you model is extends of Model:
class User extends Model {
In your controller the use line have the same path of namespace line in model, if you model is in default path, should be like this:
use App\User;
Remember that: the App is the namespace of your project, if you used the php artisan app:name command your namespace is no longer an App, and you should change it if you have the old name somewhere. If you using linux, remeber of the case sensitive.
If it does not work, put into the post the codes of the files I've listed so we can help.
in the begining
<?php
use App\User;

how can I solve "Cannot redeclare class" error in zf2?

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.

Attempted to load class "ClassName" from namespace (...). Even though namespace is imported

I have a Symfony project to which I added some non-symfony php files containing various classes. But for some reason the classes are not loaded when loading the website, even though the IDE sees them properly.
So, I have a class that needs other classes:
namespace rootspace\FrontBundle\Controller;
use rootspace\FrontBundle\Networks\TwitterOAuth;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class TwitterController extends Controller
{
public function connectAction(){
// The TwitterOAuth instance
$connection = new TwitterOAuth('abc', '123');
}
}
And then the class which fails to load (that needs yet another file)
namespace rootspace\FrontBundle\Networks;
/* Load OAuth lib. You can find it at http://oauth.net */
//require_once('OAuth.php'); -- should this be commented out?
/**
* Twitter OAuth class
*/
class TwitterOAuth {
/* Contains the last HTTP status code returned. */
}
Lastly, the third file
namespace rootspace\FrontBundle\Networks;
use Symfony\Component\Config\Definition\Exception\Exception;
class OAuthConsumer
{
public $key;
public $secret;
}
(...)
I assume the actual filenames don't matter, right? Nor their structure? PhpStorm sees all the classes properly, I can right-click through them, but it fails when deployed.
Thanks for help
Edit - the whole error message says
Attempted to load class "TwitterOAuth" from namespace "rootspace\FrontBundle\Networks" in D:\Dropbox\project\src\rootspace\FrontBundle\Controller\TwitterController.php line 15. Do you need to "use" it from another namespace?
This is because Symfony's autoloader follows PSR standards (PSR-0, PSR-4) which says that fully qualified (with namespace) class name translates to file location and name. So in fact file names does matter.
So in your case rootspace\FrontBundle\Networks\TwitterOAuth class should be located in rootspace/FrontBundle/Networks directory in file called TwitterOAuth.php
If classes you are using does not follow PSR standards you can also register them manually in app/autoloader.php file
Check these for more info:
How can I add a namespace to Symfony 2.1?:
How to autoload class
And check this answer
I forgot to add a .php extension to my filename

Categories