I'm following a tutorial on how to create laravel packages.
I'm using Laravel v8.42.1 (PHP v7.4.3) and jetstream package.
I'm stuck on creating a controller for my package, I always get following error when trying to connect to my url via the laraval app (<base-url/playground):
Target class [TestVendor\TestPackage\Http\Controllers\PlaygroundController] does not exist.
The TestVendor\TestPackage\src\routes.php is recognized by the main application:
use TestVendor\TestPackage\Http\Controllers\PlaygroundController;
use Illuminate\Support\Facades\Route;
Route::get('/playground', [PlaygroundController::class, 'index']);
And is loaded from my ServiceProvider class:
$this->loadViewsFrom(__DIR__.'/resources/views', 'playground');
loadRoutesFrom(__DIR__.'/routes.php');
My namespacing is also normally correctly written in the composer.json of my package:
"autoload": {
"psr-4": {
"TestVendor\\TestPackage\\": "src/"
}
},
And I have my PlaygroundController in src/Http/Controllers/PlaygroundController.php:
namespace TestVendor\TestPackage\Http\Controllers;
use Illuminate\Routing\Controller;
class PlaygroundController extends Controller
{
public function index()
{
return view('playground::hello');
}
}
The view is also in the right package.
I'm using https://github.com/Jeroen-G/laravel-packager to scaffold my packages.
Did multiple composer auto-load and composer updates.
It seems my controller is not recognized in the main application, I think somewhere my name spacing is not correct?
I already had a look at:
Target class controller does not exist - Laravel 8
Solution did not work
Target class does not exist. problem in laravel 8
Solution did not work
You have namespaced your controller as:
namespace TestVendor\TestPackage\Http\Controllers;
In the line above though you say:
And I have my PlaygroundController in src/Http/PlaygroundController.php:
Unless that is a typo, you need to add a Controllers directory underneath Http and put your PlaygroundController in there:
src/Http/Controllers/PlaygroundController.php
For psr-4 autoloading, your folder structure and namespaces should mimic each other.
Related
So I've been asked to create a new project in Laravel (5.4). The project will need to load in a "platform" which is also built in Laravel 5.4 and contains already written controllers and models.
I am currently autoloading the platform in composer as so :
"psr-4": {
"App\\": "app/",
"Frontend\\": "app/Modules/Frontend/",
"Admin\\": "app/Modules/Admin/",
"Platform\\": "../platform/Modules/"
}
I have a routes file with the following in it :
Route::get('/', 'HomeController#index')->name('home');
Then inside of my HomeController, I'm trying to load the platforms' HomeController.
use Platform\Modules\Frontend\Controllers\HomeController as HC;
I am then presented with the error
Class 'Platform\Modules\Frontend\Controllers\HomeController' not found
Is this at all possible to do, wise to do? I'm open to any form of suggestions.
I have managed to mimic your situation in 2 PHP hello world projects.
The problem here is that your 2 projects are using the same namespace App and when you try to autoload the 2nd project into the namespace Platform, you get Class not found error. This is because your PSR-4 namespace for Platform doesn't correspond the namespace in your 2nd project.
App\Modules\Frontend\Controllers is never Platform\Modules\Frontend\Controllers. Those are totally 2 different namespaces.
Hopefully, there's a solution for this by providing an array of paths to the common namespace in both projects.
"psr-4": {
"App\\": ["app/", "../platform/Modules/"],
"Frontend\\": "app/Modules/Frontend/",
"Admin\\": "app/Modules/Admin/"
}
In your HomeController, load your controller like this:
use App\Modules\Frontend\Controllers as HC;
Always double check running the command below if you get Class not found error:
composer dump-autoload
I am using Slim PHP as the underlaying framework for my app and composer autoload for the loading of my classes. In composer i have this psr-4 configuration:
"psr-4": {
"App\\": "app/classes/"
}
All my classes are in /app/classes/ folder.
In my classes folder i have all common classes but module specific classes i have in sub folders.
From my ../classes/connect.php file i am having this method under the corresponding namespace:
namespace App;
class connect
{
public function authenticate(){
office\AuthenticationManager::acquireAppToken(200);
}
}
In my subfolder ../classes/office/ i have a file called office.php with this method:
namespace App\office;
class AuthenticationManager
{
public function acquireAppToken($tid)
{
\App\APIManager\RequestManager::sendPostRequest();
}
}
The above method is calling another method which is located in ../classes/api.php
The namespace in that file is
namespace App\APIManager;
The problem is that when calling on this last method i get the following error:
"\App\APIManager\RequestManager" not found...
I already tried to solve this with composer dump-autoload, but it did not help. Why is this error happening?
If you want to have a class named RequestManager in App\APIManager namespace, then it must be located in APIManager/RequestManager.php file.
It also applies to the AuthenticationManager class, which should be moved to office/AuthenticationManager.php
Have a look at PSR-4 examples.
I'm working with ZendFramework3 for a project. I have a module that works
(for example : myProject/module/MyModule). Everything it's working but I would like to extends the controller which is in MyModule/src/Controller/CrudController. I mean, I have a simple module in my ZF3 project and I want the controller in this module to extends a Controller in a framework placed in myProject/vender/MyFramework/Mvc/Controller/
Problem is Fatal error: Class 'MyFramework\Mvc\Controller\ExtendedController' not found in C:\wamp\www\myProject\module\MyModule\src\Controller\MyController.php on line 7
<?php
namespace MyModule\Controller;
use MyFramework\Mvc\Controller\ExtendedController;
class MyController extends ExtendedController
{
}
I don't know what am I supposed to do to find the Controller placed in a framework in the vendor directory.
If someone could help me on this, it would be a pleasure.
Currently, I based my code on what Zend does. For example, we could find this
use Zend\Mvc\Controller\AbstractActionController;
I think the controller 'MyController' which extends the ExtendedController finds the right file but the controller is probably not load or something like that
Yours -)
EDIT : My problem has been resolved. The problem was the composer.json which needed to be updated.
I added this code in composer.json file :
"autoload" : {
"psr-0" : { "MyFramework\\": "vendor/MyFramework/{folder}/"},
},
It looks like your module is not found by composer. Have a look at the composer.json file from zend-mvc, especially the autoload section. That's how composer knows how to register packages.
AFAIK you can't just place a package in the vendor dir. You need to create a complete composer package and add it to your composer.json require section.
What might be easier during development is placing your module inside the ./module/ dir. You can than edit your module and when it's ready you publish it via packagist or a private composer repository.
To get this working you need to add MyFramework to the autoload section of your project:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"MyFramework\\": "module/MyFramework/src/"
}
},
Obviously, you need to update the autoloader with composer dump-autoload after changing the autoload section.
I'm new to traits, but thought I'd give it a try. But, it doesn't seem to load.
I've created a trait within a folder under the Laravel app directory: app\Helpers called CheckPermsAgainstObjectTrait.php
Here is the trait code:
<?php
namespace App\Helpers;
trait CheckPermsAgainstObjectTrait {
function something{}
}
I try to use it in a controller as such:
<?php
namespace App\Http\Controllers;
use this&that;
use App\Helpers\CheckPermsAgainstObjectTrait;
class PolicyController extends Controller{
use CheckPermsAgainstObjectTrait;
}
Classes in that directory load fine. PHPStorm sees the trait fine. I've clear compiled aritsan and dumped autoload. I'm guessing there is something that Laravel doesn't like with the namespacing? I would hope I don't need to do any manual loading in composer -- but I'm having trouble finding any documentation to give me a hint as to what I'm screwing up.
The error:
FatalErrorException in PolicyController.php line 15:
Trait 'App\Helpers\CheckPermsAgainstObjectTrait' not found
Any thoughts?
Did you dump autoload files?
composer dump-autoload
The answer for me was that I had the wrong namespace at the top of my trait file due to working from a Laravel trait as an example. If your trait is in App/Traits/MyTrait.php then make sure your namespace is:
namespace App\Traits;
trait MyTrait
{
// ..
}
Then from the file that's including the trait:
use App\Traits\MyTrait;
class MyClass
{
use MyTrait;
// ..
}
No need to mess with config/app.php, composer.json or autoloading.
I had a very similar problem where I was getting the error Trait 'Tests\CreatesApplication' not found in '.../my_project/tests/TestCase.php' on line 9 while running tests with phpunit because I manually updated my Laravel project and must have missed some changes. This may also happen to anyone creating a trait or class in a unique namespace.
Anyways, #Devon 's comment on the original question pointed me in the right direction.
I was missing the required autoloader configuration in my composer.json file, so I checked what it should be for my version of Laravel (5.4 in this case) from the Laravel github repository.
In this case, I was not calling from a namespace within App\ (which was already in composer.json.)
Here's what I was missing from composer.json:
{
...,
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
...
}
Thanks goes to Devon! Hope this keeps someone else from pulling their hair out.
composer dumpautoload -o
this worked for me
This may happen sometime when your Trait filename is misspelled or incorrect as with the class name. Do check your file name is same as classname
This may happen sometime when your Trait filename is misspelled or incorrect as with the class name. Do check your file name is same as classname
I made a class in php with some helper methods that parse HTML files.
I'd like to use this class in my Laravel project, but I'm new to Laravel and it's not clear how to add a simple class to a Laravel 5 project.
Is this possible? Or do I need to go to all the trouble of creating a composer package for my class, hosting it somewhere, and then require it in my composer.json file. That seems like a lot of work for including a simple PHP class, and I'm hoping there's an easier way.
As it stands right now there's not a great/easy way to do this in Laravel 5 (possibly by design). The two approaches you can take are
Create a new class in the App namespace
By default Laravel 5.0 looks for App\ prefixed classes in the app/ folder, so something like this should work
#File: app/Helpers/Myclass.php
<?php
namespace App\Helpers;
class Myclass
{
}
and then create your class with
$object = new App\Helpers\Myclass;
This approach, however, relies on you creating classes in the App\ namespace, and there's some ambiguity around if the App\ namespace is owned by Laravel, or is owned by the developer of the application.
Create your own Namespace and Register as PSR-4 autoloader
A better, but more complicated, approach would be to create classes in your own namespace, and then tell Laravel about this namespace by registering a new PSR autoloader.
First, you'd create the class definition
#File: application-lib/Myclass.php
<?php
namespace Pulsestorm;
class Myclass
{
}
Notice we've created a new folder off the root folder to hold our classes named application-lib. You could name this folder anything you like, because in the next step, you're going to add a section to your composer.json file's autoloader section
#File: composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Pulsestorm\\": "application-lib/"
}
},
The section we've added is this
"Pulsestorm\\": "application-lib/"
The key to the object (Pulsestorm\) is your namespace. The value (application-lib) is the folder where composer should look for class definition files with the specified namespace.
Once you've added this to composer.json, you'll need to tell Composer to regenerate it's autoload cache files with the dumpautoload command
$ composer dumpautoload
Generating autoload files
After doing the above, you should be able to instantiate your class with
$object = new Pulsestorm\Myclass;
The "real" right way to do this would be to create a generic composer package for your helper class, and then require that composer package into your laravel project. That may, however, be more work than you care to take on for a simple library helper.
If your class is generic enough to use it in other projects, the best way is to release it as a package.
Here's how you create packages with Laravel 5: http://laravel.com/docs/5.0/packages