I changed the app/controllers to app/Controllers, add in composer.json file:
"psr-0": {
"Controllers": "app/",
"Test":"app/"
}
In my controllers, i add:
namespace Controllers;
When i was update with composer, the following error:
L:\USB\Test>composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
{"error":{"type":"ReflectionException","message":"Class RemindersController does
not exist","file":"L:\\USB\\Test\\vendor\\laravel\\framework\\src\\Illuminate\
\Routing\\ControllerInspector.php","line":28}}{"error":{"type":"ReflectionExcept
ion","message":"Class RemindersController does not exist","file":"L:\\USB\\Test
\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerInspector.php"
,"line":28}}
since you added a namespace "Controllers" to classes that did not previously have them, did you also update all of your routes that use the controller's name?
for example:
Route::controller('reminders', '\Controllers\RemindersController');
and
route(\Controllers\RemindersController#getIndex);
Related
I'm very new in the field and I'm trying to create my first composer package. I'm following the structure mentioned here but for some reason I always get that the class is not found.
My directory structure is
Project
- src/
-- project
index.php
- vendor/
-- composer/
autoload.php
index.php
So in the main directory Project I have index.php with
<?php
use App\project;
// Autoload files using the Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';
$entry = new simplePrint();
echo($entry->printHome());
In the directory src/project/ I have index.php with
<?php
namespace App\project;
class simplePrint {
public function printHome() {
return "Hey";
}
}
in composer.json
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
After I create the files, I've made
composer install
composer dump-autoload
What I'm missing here?
Update: after composer update it is still same. The output of the composer update
$ composer update
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
127.0.0.1:45046 [500]: GET / - Uncaught Error: Class "App\project" not found in ...
PSR-4 says following: The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
So you must call your filename simplePrint.php and not index.php.
Here you can read some more information about PSR-4: https://www.php-fig.org/psr/psr-4/
When creating a package in Laravel, the packages use "\Illuminate\Support\ServiceProvider" in their *ServiceProvider.php. Which is located in the package \vendor directory.
public function boot()
{
$this->package('faiawuks/articles');
include __DIR__.'/../../../routes.php';
}
As you see I need Illuminate: $this->package to register my package specific routes.
I've noticed that Illuminate also exists inside the main vendor directory. Is it possible to remove the Illuminate vendor package for my created workbench package and use the main vendor\Illuminate package? I'ts going to be a private workbench package anyway?
I want to create 5+ packages for my application, so I can split it into 'modules'.
Solved it for now by creating my own directory structure in the directory ./modules and using this in composer.json:
"psr-0": {
"Articles": "modules/"
}
inside the "autoload": { part. So my structure is as following:
mylaravelproject
- modules
- Articles
- *othermodules
The PSR-0 autoloader looks for classes inside ./modules/Articles. The namespace to use in the Articles directory is:
namespace Articles;
I am developing a package and I want to store it in the vendor directory before I publish it.
So, The path to say Basset's service provider once it is installed via composer is
/siteroot/vendor/jasonlewis/basset/src/Basset/BassetServiceProvider.php
which composer maps in it's autoloader fine, now If mine is at
/siteroot/vendor/hailwood/databaseconfigloader/src/DatabaseConfigLoader/DatabaseConfigLoaderServiceProvider.php
What do I need to add to the composer.json file to make it generate the autoload mapping correctly?
I can manually edit the autoload_namespaces.php file to add the mapping
'Hailwood\\DatabaseConfigLoader' => $vendorDir . '/hailwood/databaseconfigloader/src/'
and that works, but obviously gets overwritten when I do a composer update, I tried adding
"psr-0": {
"Hailwood\\DatabaseConfigLoader": "src/"
}
to the composer.json but that outputs 'Hailwood\\DatabaseConfigLoader' => $baseDir . '/src/', which doesn't work.
In state, you don't respect PSR-0. So you have two solutions :
Respect PSR-0
You must put your code in /siteroot/vendor/hailwood/databaseconfigloader/src/Hailwood/DatabaseConfigLoader/DatabaseConfigLoaderServiceProvider.php. After that, simply do :
"psr-0":
{
"Hailwood\\DatabaseConfigLoader": "vendor/hailwood/databaseconfigloader/src/"
}
Use classmap autoloading
Just try :
"classmap": ["vendor/hailwood/databaseconfigloader/src/"]
using composer in a Php project, with Twig and my own framework.
I would like to "override" the Twig_Node_Expression_GetAttr class from Twig with my own class.
Everything it's working fine, but I have to manually add in composer autoload_classmap.php file :
'Twig_Node_Expression_GetAttr' => 'ebuildy/ebuildy/src/eBuildy/Templating/Twig_Node_Expression_GetAttr.php',
How can I declare this in my composer.json description file ?
Thanks,
You can just define the classmap entry in your project's composer.json, or using PSR-0 mapping as well. See the composer docs on autoloading for details. If you define the PSR-0 namespace with a more restrictive namespace than what Twig has, then you're sure yours will take over, .e.g:
{
"autoload": {
"psr-0": {
"Twig_Node_": "path/to/src/"
}
}
}
This however only works if in this src/ dir you have a file called: src/Twig/Node/Expression/GetAttr.php.
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