How to autoload custom class in Laravel? - laravel

I wrote custom class with method that returns array.
I need to autoload this class like Auth() class in Laravel, that I could get access to it from any controller not using use

Create one custom helper file
and add function
if (! function_exists('yourcustomclass')) {
function yourcustomclass()
{
use App\Http\yourcustomclassname;
return new yourcustomclassname()
}
}
you can use yourcustomclass() function from anywhere to get yourcustomclassname class object

When accessing class/function from other namespace than you're currently in you have to use Fully-Qualified Class Name (or type use, but you don't want to do that), so instead of Auth::user() you need to write \Auth::user()
\ at the beginning means that class is located in root namespace

Why don't you write super method(s) in App\Http\Controllers\Controller?
Simply call super method(s) in the subclass which extends Controller

Related

How import custom class in controller Laravel?

I have created a new directory Library in root of Laravel.
Inside I put the file with class:
class My {
//
}
So, in controller Laravel I try to get access to this class:
App\Library\My
But Laravel does not determine this path.
This is my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use View;
use App\Library\My;
class HomeController extends Controller
{
//
}
A complete and functional example based on the posts here:
1 - Folder and file - Create a folder under app/, in this example we will create a folder called Library.
We will also inside the folder create a file with the name of your class, here we will create a class called My.
So we will have app/Library/My.php
2 - Class and method - Now, for testing, inside the class create a static method called myMethod
<?php
namespace App\Library;
class My
{
public static function myMethod()
{
return 'it\'s work!';
}
}
3 - Controller - Now at the beginning of the Controller, we will declare the namespace of your Class with use:
<?php
namespace App\Http\Controllers;
use App\Library\My;
//rest of the controller code
Finally, to create an instance of the My class, in Controller, a new statement must be used:
//rest of the controller code
public function index()
{
$whatever = new My;
return $whatever::myMethod();
}
As above, make sure it is placed in the App directory and make sure it is properly namespaced e.g.
<?php
$fOne = new \App\library\functions;
$isOk = ($fOne->isOk());
?>
You should create Library folder inside app folder
namespace App\Library\My
app folder is alrdy used psr-4
In your controller
use App\Library\My as My
It's work for me. Hope this answer is helpful
You have to properly namespace your every class.
So you can import your class with use keyword, like so
use App\Library\My;
....
$my = new My();
Or if you've conflicting class name then you can use as keyword to alias the classname while importing
use App\Library\My as MySecond;
....
$my = new MySecond();
And if you want to directly access your class within the method then you can access it like so.
$my = new \App\Library\My();
Note: The leading \ means App was declared in the global scope.

Laravel - Running method of another controller in one controller

I have UserController and PetController.
In my UserController, I have rewardUser() method.
in my PetController, I'm using the $user variable which indicates the current logged in user.
How I can run my rewardUser() method from my PetController?
I've been trying to user $user->rewardUser(); but for some reasons its not recognizing my method this way.
"Call to undefined method Illuminate\Database\Query\Builder::rewardUser()"
The best way is to use a trait.
Create a trait file, in App\Common.php, for e.g. Then copy the rewardUser() method to the trait.
Your trait file:
namespace App\Forum;
trait Common {
public function rewardUser() {
// Your code here...
}
}
Then in yourUserController.php and PetController.php, use the trait.
// UserController and PetController.php
namespace App\Http\Controllers
use App\Common; // <- Your trait
class UserController extends Controller {
use Common // <- Your trait
public function doSomething() {
// Call the method from both your controllers now.
$this-rewardUser();
}
}
You can use the straight in as many controllers as you want and you can call the method in the straight using $this->methodName().
Very simply and effective.
It seems like you are missing some structure concepts, but if you really need it, you may use the container to do so:
$userController = app()->make(UserController::class);
return app()->call([$userController, 'rewardUser']);
may be you should define the method rewardUser() in the User Model and import it with use App\User

Access an object of one class in other codeigniter

I have two controller classes in my codeigniter application, say class A and B.I just want to create an object of class A and access the functions declared in class A from class B.Something like:-
class A extends someclass
{
public function function1(){
$this->load->view('welcome_message');
}
}
}
class B extends someclass2
{
protected $object;
public function __construct()
{
parent::__construct();
$this->objectA = new A();
}
}
}
I want to access the function function1 from class B using the object objectA. How can i do this?
Please help.
Thanks
well actually this is not the proper way in codeigniter. Actually when you have common functions in and you want to use them in 2 or more controllers. The best way is to create base controller in core folder with name of MY_Contoller and extend it from CI_Contoller. Write your common function in MY_Contoller. Now you have to extend all your controllers from MY_Contoller instead of CI_Contoller. You can do the same with Model.
Cross-controller access goes against CI best practice.
Either inherit both controllers from a controller that holds this common functionality (don't forget to prefix the function with '_' so it's inaccessible via url routing) or create a library that contains your re-usable functionality. A helper can also work.

Extend CodeIgniter Base Model causes Fatal error: Class 'MY_Model' not found

I want to extend CI_Model:
<?php
class ModelBasic extends CI_Model
{
}
?>
It won't be autoloaded unless under such a path:
./application/core/MY_Model.php
It seems name of MY_Model.php is mandatory. Now my problem is that when I create MY_Model.php CodeIgniter Expects me to have a model under name of MY_Model which I want to avoid. Can I have my own custom model name without making a fake class to suppress this error?
Fatal error: Class 'MY_Model' not found in /var/www/CodeIgniter/system/core/Common.php on line 174
The quickest way to achieve naming your own custom base model(s) without any additional modification is to create the file application/core/MY_Model.php, define an empty MY_Model class, and then create your own custom class(es):
class MY_Model extends CI_Model {}
class ModelBasic extends MY_Model {
// Your code here
}
// Define more than one if you want.
class ModelComplicated extends MY_Model {
// Your code here
}
Other options include:
Extend the Loader class, and replace the model() method with your own modified version. This could be complicated (I haven't explored it fully), since the actual error is occurring in system/core/Common.php :: load_class(), which, in the model's example, is instantiating the base CI_Model class, as well as MY_Model if it finds one (which it automatically looks for by default, like other base classes).
Create / Add an autoloader that follows your own rules for loading core classes (may be complicated if you try to autoload more than just models -- really depends on how you want to set your app up).
Extend the Loader class and re-write that part of the code.

How to auto include to use own class with CodeIgniter

I am using CodeIgniter. I want to use my own class to pass as an argument inside controller functions.
Normally, I can put this class in a folder and include it to MY_Controller with its path. But I want to learn if there is a way to do this in CodeIgniter. I can't put it in libraries folder and can't use loader class because it tries to create an instance of an object, but I want to create instance whenever I want. Loader class gives an error if my own class need constructor parameters.
What is the best way to do that?
Which is the best folder to put in it?
This is very easy. Consider this example
<?php
Class Home extends CI_Controller{
public $arg1 = 1;
public $arg2 = 2;
function index($this->$arg1 , $this->$arg2){ //or function index()
//Then inside function
//$vara = $this->$arg1 , $varb = $this->$arg2;
}
}

Resources