I have created a Laravel package using the guidelines of https://laravelpackage.com/.
My package name is Company/FirstPackage.
It resides in the WebSite/packages folder of a host Laravel application.
In it I have created a controller in src/Http/Controllers:
namespace Company\FirstPackage\Controllers;
class FirstController extends Illuminate\Routing\Controller {
public function getName() {
return response()->json('hi, this is name');
}
}
And I have created a simple api.php file in the WebSite/packages/Company/FirstPackage/route directory and in it, I have this route:
use Illuminate\Support\Facades\Route;
Route::get('/name', [Company\FirstPackage\Controllers\FirstController:class, 'getName']);
Route::get('/testRoute', function() { return 'route is working'; });
The 127.0.0.1:8000/testRoute works as expected. But when I go to 127.0.0.0:8000/name I get this error:
Illuminate\Contracts\Container\BindingResolutionException Target class
[Company\FirstPackage\Controllers\FirstController] does not exist.
I have tried all suggestions in this link:
Add the namespace back manually so you can use it as you did in
Laravel 7.x and before
Use the full namespace in your route files when using the
string-syntax
Use the action syntax (recommended)
But non of them worked.
How should I fix this bug?
Update: Part of the content of the composer.json is as follow:
"autoload": {
"psr-4": {
"Company\\FirstPackage\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Company\\FirstPackage\\FirstPackageServiceProvider"
]
}
}
Related
I'm trying to implement a clean architecture in laravel, thus I'm moving my own code to a src folder.
My controller is located in src\notebook\infrastructure but when i call it from routes\web.php this way:
Route::get('/notebook', 'src\notebook\infrastructure\NotebooksController#show');
i got this error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [src\notebook\infrastructure\NotebooksController] does not exist.
http://127.0.0.1:8000/notebook
i also changed the namespace value in the class RouteServiceProvider from:
protected $namespace = 'App\Http\Controllers';
to
protected $namespace = '';
This is my notebook controller class:
namespace src\notebook\infrastructure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NotebooksController extends Controller
{
public function show($id)
{
echo 'controller from infrastructure folder';
}
}
My laravel and php version in composer.json are:
"php": "^7.2.5",
"laravel/framework": "^7.24",
I feel like i'm missing something stupid but can't figure it out what.
Did you add src folder into the autoload?
In composer.json file, you must have something like this:
"autoload": {
"psr-4": {
"App\\": "app/",
"Src\\": "src/" // add this
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After changing it run composer dump-autoload.
And also don't forget to follow psr-4 rules and use Studly case namespace and class names.
namespace Src\Notebook\Infrastructure;
I am using Laravel 5.5. I have added a custom directory inside App folder in my workspace. So, the folder structure is:
Inside App\Bishwa\Transformers there are two PHP files:
Transformer.php
LessonTransformer.php
Those files look like follows:
Transformer.php
<?php
namespace Bishwa;
abstract class Transformer {
public function transformCollection(array $items){
return array_map([$this, 'transform'], $items);
}
public abstract function transform($item);
}
LessonTransformer.php
<?php
namespace Bishwa;
class LessonTransformer extends Transformer {
public function transform($lesson){
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean)$lesson['some_bool']
];
}
}
Then Inside LessonsController.php I have the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Lesson;
use Bishwa\LessonTransformer;
class LessonsController extends Controller
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
dd('ok');
}
While running action of the controller, It gave me an error message saying:
Reflection Exception: Class Bishwa\LessonTransformer does not exist
I have tried composer dump-autoload, restarting the server again but none of them helped. Am I doing wrong while Namespacing or What?
Change your namespace of the files in your custom directory to App\Bishwa.
Well thanks to Jerodev and Jack. Since, both of them were write I decided to write myself a combined solution to this problem.
1st Solution:
In case of custom namespaces and custom classes I have to include the path to classname in Composer.json file in the following portion:
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Bishwa/Transformers"
],
"psr-4": {
"App\\": "app/"
}
2nd Solution:
Changing NameSpace of my files to custom directory App\Bishwa .
Namespace of transformer.php and LessonTransformer.php now becomes:
namespace App\Bishwa\Transformers;
While using in LessonsController:
use App\Bishwa\Transformers\LessonTransformer;
Once again, big thanks to Jerodev and Jack. Its my silly mistake, that I couldn't figure that out.
I'm trying to extend Laravel 5 core class. What i want to achieve is that i can have custom url generators eg. URL::test(), will generate custom link.
So far i have:
Created app/Acme/lib folder
Added app/Acme/lib path to composer.json classmap
"autoload": {
"classmap": [
....
app/Acme/lib
]
}
Created custom UrlGenerator class in Acme/lib/CustomUrlGenerator.php
<?php namespace App\Acme\lib;
use \Illuminate\Routing\UrlGenerator;
class CustomUrlGenerator extends UrlGenerator {
public function test() {
return $this->to('/test');
}
}
Created service provider app/Acme/lib/CustomUrlServiceProvider.php
<?php namespace App\Acme\lib;
use \Illuminate\Routing\RoutingServiceProvider;
class CustomUrlServiceProvider extends RoutingServiceProvider {
public function boot() {
App::bind('url', function() {
return new CustomUrlGenerator(
App::make('router')->getRoutes(),
App::make('request')
);
});
parent::boot();
}
}
Registered service provider in app/config/app.php
Run composer dump-autoload
Now when i run {!! URL::test() !!}, im getting 404 for every route
Sorry, the page you are looking for could not be found.
NotFoundHttpException in /vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php line 143:
Is there something that i'm missing?
Many thanks for any help ..
You talk about a mistake in the RouteCollection.php file, but you don't include it in your question. Furthermore, I would have written differently in the composer.json, like this:
"autoload": {
"classmap": [
// ....
"App\\Your_Namespace\\" : "app/Acme/lib",
]
}
Just started working through laracasts and trying to move on from direct eloquent use in the controllers.
I have implemented everything that I need to but hitting this error:
Class tva\Repositories\VehicleRepositoryInterface does not exist
My folder structure is:
app/
tva/
repositories/
VehiclesController:
use tva\Repositories\VehicleRepositoryInterface;
class VehiclesController extends \BaseController {
protected $vehicle;
public function __construct(VehicleRepositoryInterface $vehicle)
{
$this->vehicle = $vehicle;
}
}
In the repositories folder:
VehicleRepository:
namespace tva\Repositories;
class VehicleRepository implements VehicleRepositoryInterface {
}
VehicleRepositoryInterface:
namespace tva\Repositories;
interface VehicleRepositoryInterface {
}
And also updated my composer.json:
"psr-0": {
"tva": "app/"
},
To me, this should work?
Issue solved, instead of using psr-0 I added the directory to the classmap and all issues solved.
In my folder "/Vendor/User/Admin" I created a new custom class (Adminuser.php)
namespace \User\Admin;
class Adminuser {
public $username;
public $password;
}
Now Im trying to use it in a controller:
namespace Section\AdminBundle\Controller;
use \User\Admin;
class DefaultController extends Controller
{
public function indexAction()
{
$AdminUser = new \User\Admin\Adminuser(); // CLASS NOT FOUND!!
.......
Why is this happening?, the namespace is wrong? (I tried a few options..)
Im very begginer with Symfony, sorry.
You have 2 main issues.
The First
When declaring a namespace you should not start with a \
namespace \User\Admin;
Should just be:
namespace User\Admin;
The Second
If you want those classes to live in your Vendors Dir then you need to make sure the class is being autoloaded by symfony correctly. To do this we will use composer.
In your composer.json you will want to change this section from:
"autoload": {
"psr-0": { "": "src/" }
},
TO:
"autoload": {
"psr-0": { "": "src/" },
"psr-0": { "": "vendor/User/Admin" }
},
Then composer will add classes under that folder to the available namespaces and you will be able to access it as expected.
just remove the first "\" in the namespace, as the comments said. So the first file is:
namespace User\Admin;
class Adminuser {
public $username;
public $password;
}
if the problem persist check your autoloading configuration, maybe the right way would be using src dir to develop your code, not vendor :S