I have this composer config:
under classmap: "app/models"
under psr-4: "App\\Models\\": "app/models"
"autoload": {
"classmap": [
"database",
"app/models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "app/models"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
Here is my model:
namespace App\Models;
class TemplateRow extends Model{
protected $table = "template_rows";
}
in my controller i did: $row = new TemplateRow(); and I got class not found exception.
I did dump-autoload.
Thanks
You have to actually reference the model with it's namespace. You can either write:
$row = new \App\Models\TemplateRow();
Or add this before the class instead:
use App\Models\TemplateRow;
Also note that you shouldn't even have to add the entry under psr-4. If you're directory structure follows the namespacing. To be certain, call your folder Models and not models
Related
Hello Guys I tried To Made A Helper Function Like asset Using Laravel 9.21.6
This Is My Code
Helper.php
<?php
function show_name() {
return 'Ahmed Emmam';
}
routes.php
Route::get('test-helper' , function () {
show_name();
});
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files" : [
"app/Helpers/Helper.php"
],
The Error Is Call to undefined function show_name()
i ran Composer dump-autoload but it's failed
Can Anyone Help Me Please
You should load the other files within the autoload field.
{
"autoload": {
"psr-4": { ... },
"files": [
"app/Helpers/Helper.php"
]
}
}
https://getcomposer.org/doc/04-schema.md#files
Try to usefunction_exists function
if (!function_exists('show_name')) {
function show_name()
{
return 'Ahmed Emmam';
}
}
Composer.json
"autoload": {
"exclude-from-classmap": [
"vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
],
"psr-4": {
"App\\": "app/",
"Laravel\\Passport\\": "app/Override/" //tried "Passport//" only also
}
},
I have file AccessTokenController.php which lie in vendor\laravel\passport\src\Http\Controllers\ and i create Override folder inside App directory on where i copied that file and make changes in code
And finally when i do :
composer dump-autoload
i get error as :Class Laravel\Passport\Http\Controllers\AccessTokenController located in ./app/Override/AccessTokenController.php does not comply with psr-4 autoloading standard. Skipping.
seems problem with namespace which i couldn't figure out though trying some of the ways ..
Anyone can help me ?
I would simply override the route from within the app as mentioned here. No need for the approach you mentioned above.
However, if you want to keep going this route you can try the following.
"psr-4": {
"App\\": "app/",
}
"exclude-from-classmap": [
"Laravel\\Passport\\Http\\Controllers\\AccessTokenController"
],
"files": [
"app/Override/AccessTokenController.php"
],
I solved it by including complete namespace of AccessTokenController.php.so final code would look like this:
"autoload": {
"exclude-from-classmap": [
"vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
],
"psr-4": {
"App\\": "app/",
"Laravel\\Passport\\Http\\Controllers\\":"app/Override/"
}
}
I am having some issues with developing a package. I have read through several tutorials on the web and followed their advice but for some reason I still run into the same issue.
What I am trying to do: Develop a package in an active project
In my Laravel folder, I have a packages/companyname/laravel-model-notes. Inside that package, I have a composer.json file with the following:
{
"name": "companyname/laravel-model-notes",
"description": "A package to add notes to an eloquent model in Laravel.",
"type": "library",
"require": {},
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"CompanyName\\LaravelModelNotes\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"CompanyName\\LaravelModelNotes\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"CompanyName\\LaravelModelNotes\\NoteServiceProvider"
]
}
}
}
And then in my laravel composer.json file, I have added the following:
"autoload": {
"psr-4": {
"App\\": "app/",
"CompanyName\\LaravelModelNotes\\": "packages/companyname/laravel-model-notes/src"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Helpers/ThemeHelper.php",
"app/Helpers/BaseHelper.php"
]
},
When I manually add the service provider to the App.php things work, however artisan commands like publishing the migrations does not work. For some reason, Laravel does not see the package?
Is there some special way of building a package inside of a project and having that project recognize the package?
I am using Laravel 7.
Below is my service provider:
class NoteServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('notes.php'),
], 'config');
if (! class_exists('CreateNotesTable')) {
// TODO: If tenant folder exists publish there too.
$this->publishes([
__DIR__.'/../database/migrations/create_notes_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_notes_table.php'),
], 'migrations');
}
}
}
/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'notes');
}
}
I try to create a package but for some reason, I cannot access the front-end.
This is my package structure, from the project root directory:
/package/contact/src/routes/web.php
/package/contact/composer.json
/package/contact/ContactServiceProvider
The service provider class looks like the following:
namespace Sidneylab\Contact;
use Illuminate\Support\ServiceProvider;
class ContactServiceProvider extends ServiceProvider
{
public function boot(){
$this->loadRoutesFrom(__DIR__.'/routes/web.php');
}
public function register(){
}
}
Composer.json
"name": "sidneylab/contact",
"description": "This will send email to admin and send contact query to database",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Sidney de Sousa",
"email": "esp.sousa#gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Sidneylab\\Contact\\": "src/"
}
},
web.php
Route::get('contact', function(){
return "contact";
});
In my main composer.json file I added this:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Sidneylab\\Contact\\": "package/contact/src"
}
},
Also, I imported the service provider inside my confi/app.php
Sidneylab\Contact\ContactServiceProvider::class
Now, when I hit my url/contact it does not return anything. Anything I could have possibly missed?
Anything to do with the things I named?
I've a helper file helper.php where I keep some helper functions.
//helper.php
function isAuthLiked($authLikedPosts, $post)
{
return !! Auth::check() && $authLikedPosts->contains('id', $post->id);
}
Now in my test case, I wrote:
$this->assertTrue(isAuthLiked($authrenominations, $post[0]));
When I ran the test case, I get the error:
Fatal error: Call to undefined function isAuthLiked() in
C:\wamp\www\Nom7\tests\integration\UserTest.php on line 304
I've added the helper file in the compose.json auto-load. But the problem persists.
"autoload": {
"classmap": [
"database",
"app/Http/Controllers",
"app/Models"
],
"files":[
"app/helper.php"
],
"psr-4": {
"App\\": "app/",
"Acme\\": "app/Acme/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"files":[
"app/helper.php"
]
},
Check your path. You are using relative paths in your files array. You are running your tests from your tests directory, so cannot find app/helper.php