Laravel could not autoload a model from within Acme Directory? - php

in my Laravel app I have created Acme\fooDir directory inside app directory
i am trying to call a model -"barModel"- from within the fooDir
//app/Acme/fooDir/test.php
$barM = new barModel;
but i am getting this error
Class 'Acme\fooDir\barModel' not found
here is my app structure
app
-app/Acme
--app/Acme/fooDir
---app/Acme/fooDir/test.php (this is the file that could load the barModel)
-app/models
--app/model/barModel.php (this is the model i am trying to use)
I have added autoload in composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0":
{
"Acme": "app/"
}
and
I have ran the command
composer dump-autoload -o
and
php artisan dump-autoload
but the problem not solved and i am still getting the same error
Class 'Acme\fooDir\barModel' not found
any idea?

When using namespace all your classes are called within that namespace.
If you want to use barModel in Acme/fooDir/test.php your will need to use use barModel; just after your namespace row.
<?php
namespace Acme\fooDir;
use barModel;
class test {
}

Two things.
1, Have you namespaced your classes ie,
<?php namespace Acme\FooDir;
class Test ...
2, Are you using use in your class
<?php namespace Acme\FooDir;
use \BarModel
class Test ...
?

Would this do the trick for you.
$barM = new \barModel;
PHP namespaces and importing

Related

Adding Third party library to Laravel

I have an RSA algorithm Library giving to me by a payment gateway and When I do a
include (app_path().'/PaymentGateway/Crypt/RSA.php');
this and try to make an object as $rsa = new Crypt_RSA(); this it gives me and error saying
Class 'App\Http\Controllers\Crypt_RSA' not found
I tried including it in web.php and making an object it worked the problem occur when I try to include it in a Controller.
This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.
I am using jpgraph 4.1 on Laravel 5.5 using PHP 7.
Created a folder under app called jpgraph
Placed the src folder that is in the tarball of jpgraph in that folder
Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJM in the jpgraph folder.
In composer.json added "app/jpgraph/Graph1.php" to the "classmap"
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/jpgraph/Graph1.php"
],
"psr-4": {
"App\\": "app/"
}
},
In the application folder:
composer dump-autoload
Checked the autoload_classmap.php and I have
'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',
In my Model at the top I have
use Custom_GraphsJM;
To create a class
$Two_Graphs_Temp = new Custom_GraphsJM();
You can tell Composer to autoload any (non-PSR) class by adding the base folder to:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
....
And you can also autoload autoloaders by adding them to the files section:
"autoload": {
"files": [
"temboo/src/Temboo_Loader.php"
],
...
After adding those entries, execute:
composer dumpautoload
And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.
On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.json of your project:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
...
The only thing you will need to do is simply use the namespace:
use App/Path/To/Third/Party/plugin/Class;
If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:
"psr-4": {
"ProjectRootNs\\": "projects/myproject/"
}

Error Class 'App\\Helpers\\SomeClass' not found in laravel 4 [duplicate]

I have the following error while trying to run my controller
Controller class not found
I have this code in my routes.php file
Route::get('cms/create-page', 'AdminCMSController#create_page');
Route::post('cms/createpage','AdminCMSController#createpage');
Route::controller('cms','AdminCMSController');
And this is the code in my Controller
class AdminCMSController extends BaseController {
public function create_page() {
}
public function createpage() {
}
}
How can I fix it?
If you didn't move the controllers directory from the original location (which is «project_root»/app/controllers/, you must guarantee that:
Laravel's autoload has the controller directory. Navigate to «project_root»/app/start/global.php. You need to have something like this:
(...)
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
(...)
Take notice to this line app_path().'/controllers'. It must exist.
Also, open your composer.json file and verify that the following lines exist:
(...)
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
(...)
Make sure that you have the line with app/controllers
If you have this lines and you still get the same message, go to your project root and run the following command from the command-line composer dumpautoload -o.
Laravel works with Composer, which is a dependency management tool for PHP. It also prepares an autoload file for all your project classes (see composer docs). When you run the composer dumpautoload command, it will create some files within «project_root»/vendor/composer.
Make sure that you can find the class AdminCMSController in the file «project_root»/vendor/composer/autoload_classmap.php. You should see something like this:
'AdminCMSController' => $baseDir . '/app/controllers/AdminCMSController.php',
If you have changed the default location of your controllers directory, you have to do either one of the following steps. However, since you are not defining a namespace in your class, it doesnt seem likely that this is your problem:
Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:
/app
/commands
/config
/database
/Acme
/controllers
You have to specify the Acme folder in your composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Acme": "app/"
}
},
After this you need to update you composer autoload files with the command composer dumpautoload.
If you do not want to use the PSR-0 for autoloading, you need to change your routes file from this
Route::controller('cms','AdminCMSController');
to this:
Route::controller('cms','Acme\controllers\AdminCMSController');
IF you use PSR-0, you need to namespace your classes like this:
<?php namespace Acme\controllers;
class AdminCMSController extends BaseController {
(...)
}
Curious about the Acme reference? I was too. Refer to the wikipedia.

Controller class not found in Laravel 4

I have the following error while trying to run my controller
Controller class not found
I have this code in my routes.php file
Route::get('cms/create-page', 'AdminCMSController#create_page');
Route::post('cms/createpage','AdminCMSController#createpage');
Route::controller('cms','AdminCMSController');
And this is the code in my Controller
class AdminCMSController extends BaseController {
public function create_page() {
}
public function createpage() {
}
}
How can I fix it?
If you didn't move the controllers directory from the original location (which is «project_root»/app/controllers/, you must guarantee that:
Laravel's autoload has the controller directory. Navigate to «project_root»/app/start/global.php. You need to have something like this:
(...)
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
(...)
Take notice to this line app_path().'/controllers'. It must exist.
Also, open your composer.json file and verify that the following lines exist:
(...)
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
(...)
Make sure that you have the line with app/controllers
If you have this lines and you still get the same message, go to your project root and run the following command from the command-line composer dumpautoload -o.
Laravel works with Composer, which is a dependency management tool for PHP. It also prepares an autoload file for all your project classes (see composer docs). When you run the composer dumpautoload command, it will create some files within «project_root»/vendor/composer.
Make sure that you can find the class AdminCMSController in the file «project_root»/vendor/composer/autoload_classmap.php. You should see something like this:
'AdminCMSController' => $baseDir . '/app/controllers/AdminCMSController.php',
If you have changed the default location of your controllers directory, you have to do either one of the following steps. However, since you are not defining a namespace in your class, it doesnt seem likely that this is your problem:
Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:
/app
/commands
/config
/database
/Acme
/controllers
You have to specify the Acme folder in your composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Acme": "app/"
}
},
After this you need to update you composer autoload files with the command composer dumpautoload.
If you do not want to use the PSR-0 for autoloading, you need to change your routes file from this
Route::controller('cms','AdminCMSController');
to this:
Route::controller('cms','Acme\controllers\AdminCMSController');
IF you use PSR-0, you need to namespace your classes like this:
<?php namespace Acme\controllers;
class AdminCMSController extends BaseController {
(...)
}
Curious about the Acme reference? I was too. Refer to the wikipedia.

Can Laravels ioc container use autoloaded namespaces?

Lets say I have a class MyClass in app/repository/MyClass.php in the Repository namespace:
namespace Repository;
class MyClass { ... }
I can bind this using the ioc container:
App::bind('SomeClass', 'Repository\MyClass');
Same thing with model dependencies:
SomeClass extends Eloquent{
public function dependency()
{
return $this->hasOne('Models\Dependency');
}
}
One solution is to create aliases in app.php, but this has to be done for every single file that I want to have automatically namespaced:
'MyClass' => 'Repository\MyClass'
Is it possible to make the ioc container recognize the correct namespace for the classes without using aliases? Can we in any way use Composer for this?
It is not possible unless you want to register an alias for every class in your namespaces. My recent Laravel 4 project has IOC Container bindings to namespaced classes which I declared explicitly.
App::bind('Foo\Bar\Repositories\UserRepositoryInterface', 'Foo\Bar\Repositories\DbUserRepository');
You could use
cd /path/to/laravel/
php composer dump-autoload
to have laravel see all the (new) classes and load them correctly
Add app/repository to the autoload section of the composer.json file, like this for example:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/repository"
]
},
Then run composer dump-autoload.

Custom helper class not loading in Laravel 4

I am currently running into a problem when trying to use a custom helper class in Laravel 4.
I've created a folder in app/libraries which has a custom class MenuComposer.
app/libraries/folder/MenuComposer.php
<?php
namespace 'folder\MenuComposer'
class MenuComposer {
// Code here
}
I've edited composer.json to autoload the app/libraries folder and ran the dump-autoload command in console.
composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries"
]
},
And finally I call the class like so:
View::composer('layouts.back', 'folder/MenuComposer');
Whatever I try, Laravel keeps returning the message Class 'MenuComposer' not found
Does anyone here know what the problem might be?
Your namespace should be declared as the following rather than with quotes:
namespace folder\MenuComposer;
Composer dump-autoload then generates the following in your "/vendor/composer/autoload_classmap":
'folder\\MenuComposer\\MenuComposer' => $baseDir . '/app/libraries/folder/MenuComposer.php'
Which would indicate the class can be reached at:
folder/MenuComposer/MenuComposer
Hope this helps!

Categories