I am trying to package my extending controller into my package. So, I put all my controllers in 'controllers' under 'src' folder.
MyController.php
namespace MyVendor\MyPackage;
use \Illuminate\Routing\Controller;
class MyController extends Controller
{
public function loginAction()
{
}
}
I tried to call it from route in package with MyVendor\MyPackage\MyController#loginAction and it end up with the message "Class MyVendor\MyPackage\MyController does not exist".
What did I missed or done wrong? How to make it works?
Thank you.
Step 1: make sure it is being autoloaded via composer. In composer.json (in your workbench/package):
"autoload": {
// ...
"classmap": [
"src/controllers",
],
// ...
},
Then run composer dump-autoload from the command line, but make sure you are in the package directory (e.g. workbench/name/package/)!
Step 2: add an alias in /app/config/app.php.
Related
I'm creating a composer installable project inside vendor.
This is my service provider file,
<?php
namespace vimuths123\gitpack;
use Illuminate\Support\ServiceProvider;
class GitpackServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('gitpack', function ($app) {
return new Gitpack;
});
}
public function boot() {
// loading the routes file
require __DIR__ . '/Http/routes.php';
// define the path for the view files
$this->loadViewsFrom(__DIR__ . '/../views', 'gitpack');
}
}
This is the structure,
vendor
|
vimuths123
|-gitpack
|-src
| |-GitpackServiceProvider.php
|
|-composer.json
I already added my service provider in app/config.php
vimuths123\gitpack\GitpackServiceProvider::class,
and my root composer.json I have following code.
"psr-4": {
"App\\": "app/",
"vimuths123\\gitpack\\" : "vendor/vimuths123/gitpack/src"
}
This is my package composer file,
{
"name": "vimuths123/gitpack",
"autoload": {
"psr-4" : {
"vimuths123\\gitpack\\" : "src"
}
},
"require": {
"composer/installers": "~1.2"
}
}
but all I'm getting is this error,
Class 'vimuths123\gitpack\GitpackServiceProvider' not found
It would be great help someone can help me on this.
You should not put any files into vendor/ by hand. If you are developing a library it must be composer installable library (which once installed end up in vendor/.
Your composer.json seems wrong, especially vendor/vimuths123/gitpack/src name space in psr4. This's smells from a mile as I'd bet you not using vendor/vimuths123/gitpack/src namespace.
Finally, after adding new class you should update class loader to let it know about that:
composer dumpautoload
which solves most of problems with "cannot find my class" issues.
EDIT
It seems your problems are in your library package, not the project using it. From comments it looks that you need to edit your package's composer.json. Assuming package is using vimuths123\gitpack namespace (note, namespace does NOT have to be the same as package name - these are two different things) and its sources sit in src subfolder (so it would be <project>/vendor/vimuths123/gitpack/src) then I'd rework autoload section to look like this:
"autoload": {
"psr-4" : {
"vimuths123\\gitpack\\" : "src"
}
}
and then composer dumpautoload.
Run the following artisan command:
php artisan optimize
Then see if the class can be found by Laravel.
I am developing a blog as a custom package for Laravel 5.3.
So far I have got the routes, controller, models and migrations working.
I am now working on the CRUD. For the forms I thought I could use this:
https://laravelcollective.com/docs/5.3/html
UPDATE
I have installed the package with composer. The dependency is in the composer.json file. The files are in my package's vendor folder.
I have also tried to run composer dump-autoload -o
From my composer.json
"require": {
"laravelcollective/html": "5.3.*"
}
I have looked everywhere and people suggest to do the following:
/**
* Register bindings in the container.
*
* #return void
*/
public function register()
{
// Bind the app.
$this->app->bind('blog', function ($app) {
return new Blog;
});
// Register LaravelCollective Form Builder.
$this->app->register('Collective\Html\HtmlServiceProvider');
$this->app->alias('Form', 'Collective\Html\FormFacade');
$this->app->alias('Html', 'Collective\Html\HtmlFacade');
}
I currently get the following error everywhere:
FatalThrowableError in Application.php line 610:
Class 'Collective\Html\HtmlServiceProvider' not found
Really not sure where I am going wrong.
UPDATE
I have tried bind instead of register:
$this->app->bind('Collective\Html\HtmlServiceProvider');
But this time I get the following error:
Class 'Collective\Html\FormFacade' not found
Make sure your composer.json contains the dependency laravelcollective/html, if not, add it to your composer.json or include it via the following command composer require laravelcollective/html
Make sure you've added the dependency to providers and aliases in config/app.php
'providers'=>[
Collective\Html\HtmlServiceProvider::class,
],
'aliases'=>[
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
],
Created the following file:
File: App\Services\Custom\Auth\AuthService.php
Name space: App\Services\Custom\Auth
Class name: AuthCustom
Method inside: foo()
In my controller I'm trying to call the foo method from the Service I created.
App\Services\Custom\Auth\AuthService\AuthCustom::foo()
Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found
What am I doing wrong?
Thanks!!
EDIT:
I added this in the composer.json and run composer dump-autoload without errors.
And it works!
"autoload": {
"classmap": [
"database",
"app/Services/Custom/Auth/AuthService.php"
],
"psr-4": {
"App\\": "app/"
}
},
Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.
Once you fix this, make sure you do a composer dump-autoload on the command line.
It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.
The composer.json is very important for autoloading!
Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload
It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/
I follow the docs: http://laravel.com/docs/master/migrations#database-seeding
I placed UserTableSeeder file near DatabaseSeeder. In resources/database/seeds/ folder.
These files are without namespaces (only classes in app/ are namespaced).
Of course there is an exception: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'
What is the best way to solve this problem?
The default Laravel 5 project has a classmap defined in its composer.json:
{
// ...
"autoload": {
"classmap": [
"database"
],
// ...
}
}
Run composer dump every time you add or remove a class on your database directory to update the Composer autoloader
Reference: https://github.com/laravel/laravel/blob/develop/composer.json
You should use composer dump-autoload command. From the docs:
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:
composer dump-autoload
Reference here.
I have created a package following the "Creating a Package" instructions in the Laravel 4 documentation. After creating the package I have created a "controllers" folder and a routes file. The new file structure is:
/src
/Vendor
/Package
PackageServiceProvider.php
/config
/controllers
/lang
/migrations
/views
routes.php
/tests
/public
I added the routes file to the boot portion of the package service provider:
public function boot()
{
$this->package('vendor/package');
include __DIR__ . '/../../routes.php';
}
Then added a basic route to the routes file:
Route::get('/package', function() {
return "Package route test";
});
Visiting my application at the specified route (app.dev/package) returns the expected:
Package route test
Then adding a basic controller call to the route (using the default Laravel controller, "HomeController") works:
Route::get('/package', 'HomeController#showWelcome');
I then followed this SO answer for setting up the controller for the package. I added the src/controllers folder to the composer classmap, then I dumped the autoloader and checked vendor/composer/autoload_classmap.php and found the class is successfully loaded by composer:
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'HomeController' => $baseDir . '/src/controllers/HomeController.php',
);
Now I added the new package controller to the route using the namespace:
Route::get('/package', 'Vendor\Package\Controllers\HomeController#showWelcome');
but this produces an error about not finding the class:
ReflectionException: Class Vendor\Package\Controllers\HomeController does not exist
I've also tried calling it using the package name:
Route::get('/package', 'Package::HomeController#showWelcome');
which produces the same error:
ReflectionException: Class Vendor\Package\Controllers\HomeController does not exist
No matter what I try the package cannot access its own controller, which composer confirms is loaded (by viewing vendor/package/autoload_classmap.php).
Any ideas? I'm not sure if the issue is composer not loading the class, I'm not sure where to start with debugging the problem. I've created another package and repeated the steps here and get the same problem.
I can access the package views from both the package and the app, eg:
View::make('package::view');
The problem seems to be between composer loading the controller and Laravel being able to access it.
The mistake was including the controllers path in the route. I had the following:
Route::get('/package', 'Vendor\Package\Controllers\HomeController#showWelcome');
The correct usage is:
Route::get('/package', 'Vendor\Package\HomeController#showWelcome');
With the namespace included in the controller:
namespace Vendor\Package;
Controller should extend illuminate:
\Illuminate\Routing\Controllers\Controller
Still can't use the package name (eg: Package::HomeController#showWelcome), but I can using the namespace. yay.
Problem solved.
You may try edit your Vendor/Package/composer.json and insert the controllers dir to autoload/classmap:
....
"autoload": {
"classmap": [
"src/migrations",
"src/controllers",
"src/models"
],
"psr-0": {
"Package\\Controller": "src/"
}
}
....
After that, open your terminal and from your package root dir do a composer dump-autoload
Works for me...
have a look into this git article might be of help
https://github.com/jaiwalker/setup-laravel4-package