I would like to include a class for every controller to use. Here is what I tried with no luck.
In the Controller.php file, I added this line of code:
use App\Lib\RequestType;
Then in my controller UserController.php, I called a test function.
dd(RequestType::test());
But a fatal error is thrown.
Class 'App\Http\Controllers\RequestType' not found
What is laravel looking for the class in the Controllers folder. Shouldn't UserController inherit the Controller class?
Thanks in advance
If you are trying to write a static class and call it by use, then one option is to make use of autoload feature of composer, which will enable the class to be used anywhere.
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"app/Lib"
]
},
Then in any controller
use RequestType;
then
dd(RequestType::test());
Note: Function test should be like
public static function test()
There is no way to auto-magically have a class everywhere.
PHP use is not inherited by extending classes. Even Laravel's Facades have to specified.
You can store the class in the App\Http\Controllers\Controller->__contruct() to make it more easily manageable.
// App\Http\Controllers\Controller
public function __construct()
{
$this->requestType = new RequestType();
}
// App\Http\Controllers\FooController
public function __construct()
{
parent::__contruct();
}
public function index()
{
$requestType = $this->requestType;
}
Related
I'm facing a problem with my slim(http://www.slimframework.com/) application. When I'm trying to load my Database class using namespacing I get this error:
Message: Class 'Craft\Code\CraftDB\Database' not found
I have Database.php in folder app/config/Database.php
My Database class start like this
namespace Craft\Code\CraftDB;
class Database {
I'm trying to use it like this in another file:
use Craft\Code\CraftDB as DB;
class MyOtherClass {
protected $connectDb;
protected $db;
public function __construct() {
$this->connectDb = new DB\Database;
$this->db = $connectDb->connect();
}
My Composer file is :
"autoload": {
"psr-4": {
"Craft\\Code\\": "app/"
}
}
I'm trying to find the problem but I'm lost now. Please help. Thanks
Where you say your file is: app/config/Database.php
Where you tell Composer your file is: app/CraftDB/Database.php
Things simply don't seem to match here. On file system side you have that config level unaccounted for, on namespace side CraftDB level in namespace.
From your description I imagine you need something like:
"autoload": {
"psr-4": {
"Craft\\Code\\CraftDB\\": "app/config/"
}
}
I am new in laravel.
I am writing following code in the controller method.
include_once(app_path() .'/Classes/Pgp_2fa.php');
$pgp = new Pgp_2fa();
Following errors are shown.
FatalThrowableError in AuthController.php line 146: Class
'App\Http\Controllers\Pgp_2fa' not found
You have to use the correct namespace for your class.
If you are not using any namespace, you should add a \ in front of the class to let php know that the class exists in the root namespace. Otherwise php will look in the current namespace which is App\Http\Controllers when you are in a controller.
$pgp = new \Pgp_2fa();
This is not just Laravel behavior but php in general.
When you add new classes there are couple way to use them. (I mean, load them)
If your classes has unique name and don't use namespace, then you can add backslash \ start of the class.
$class = new \Pgp_2fa;
But if you want to define them namespaces and use with same name so
many times, then you have to add these classes in composer.json. Open your
composer.json and come into "autoload" section and define class
directory in classmap
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Classes", // so all classes in this directory are going to load
],
And don't forget to say composer dump-autoload after these changes and creating new classes
If you don't want to dump your composer when you add a new class,
then you may want add those classes inside of the psr-4 section in composer
"autoload": {
"psr-4": {
"App\\": "app/",
"App\\Classes\\": "app/Classes",
}
},
you need to add namespace for your custom class
or just add slash on your class name. like :
$pgp = new \Pgp_2fa();
You can call inside the constructor like below, and you can use the
$Pgp_2fa variable in remaining function
class controllername extends Controller
{
public function __construct(Route $route,Request $request)
{
$Pgp_2fa = new \Pgp_2fa();
}
}
You can easily add your custom classes by added it to Composer's autoload array. Go to composer.json and add it to:
"autoload": {
"files": [
"app\\Classes\\Pgp_2fa.php"
]
},
Now in your app folder, under classes folder add your Pgp_2fa.php file which will have your class. You can use it anywhere you want, it should be automatically auto-load-ed.
After adding your class write command:
composer dumpautoload to refresh autoload class mapping.
For example, There is our class Common.php in a directory called Mylibs in app directory.
app/mylibs/Common.php
we need to namespace App\Mylibs;
namespace App\Mylibs;
class Common {
public function getSite() {
return 'AmirHome.com';
}
}
Now, you can access this class using the namespace within your classes just like other Laravel classes.
use App\Mylibs\Common
in Controller:
namespace App\Http\Controllers;
use App\Mylibs\Common;
class TestController extends Controller {
protected $common;
public function __construct(Common $common) {
$this->common = $common;
}
public function index() {
echo $this->common->getSite();
}
}
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 am trying extending CodeIgniter controller in my application using composer but it's not working.
This give me
Fatal error: Class 'CI_Controller' not found in D:\xampp\htdocs\ci-dev\application\core\MY_Controller.php on line 11
i knew that if i add spl_autoload_register in my config.php then it is work but i want to use composer.
here is my all set up.
i create MY_Controller in my application/core/MY_Controller.php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
}
}
after this i add admin controller in application/libraries/Admin_Controller.php
class Admin_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
and front-end controller in application/libraries/Frontend_Controller.php
class Frontend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
This is my default controller index call
class Welcome extends Frontend_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
i set up my composer like this in config.php
$config['composer_autoload'] = FCPATH.'../vendor/autoload.php';
and composer.json file like this
"autoload": {
"files" : [
"application/core/MY_Controller.php",
"application/libraries/Admin_Controller.php",
"application/libraries/Frontend_Controller.php"
]
},
CodeIgniter loads CI_Controller after your vendor/autoload.php file, and since you're listing them under the "files" option in your composer.json, they're included immediately as opposed to right when you need them.
That's not only what causes the error, but also beats the entire purpose of using an autoloader - if you'd be explicitly listing the includes, you might as well just require_once them.
What's common in CI, is to require or even directly declare your multiple base controller classes from inside MY_Controller.php - then you know they'll be available exactly when you need them.
But if you insist on loading them through Composer, there's a work-around - list system/core/Controller.php under the autoloaded files as well.
There is a class in app/Libraries/TestClass.php with following content:
class TestClass {
public function getInfo() {
return 'test';
}
}
Now, I would like to call getInfo() method from this external class in my Controller.
How can I do such thing?
First you should make sure that this class is in the right namespace. The correct namespace here would be:
namespace App\Libraries;
class TestClass {
Then you can just use it like any other class:
$test = new TestClass();
echo $test->getInfo();
Don't forget the import at the top of the class you want to use it in:
use App\Libraries\TestClass;
In case you don't have control over the namespace or don't want to change it, add an entry to classmap in your composer.json:
"autoload": {
"classmap": [
"app/Libraries"
]
}
Then run composer dump-autoload. After that you'll be able to use it the same way as above except with a different (or no) namespace.