Laravel 4 how can I call another controller - php

I'm trying to call an external controller which is outside of App. Example path project\domains\service\onion\controllers\SignupController.php. In my App->controller got this class exampleController. So I try to get the function from SignupController.
Code
use Project\domains\service\onion\controllers\SignupController;
Class exampleController extends \RestController
{
public function grap()
{
$result = App::make('SignupController')->store();
}
}
Got error: Project\domains\service\onion\controllers\SignupController not found

You can try simply
(new SignupController())->methodOfSignupController();

Related

Codeigniter v4:Custom View Does Not Return

I'm using Codeigniter v4 and wanted to show a custom view to users, so I made a Controller named Test:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return view('custom');
}
}
And a Model named Usermodel:
<?php
namespace App\Models;
class Usermodel extends CI_Model
{
public function getusers()
{
return [
['firstmame'=>'Mohd','lastname'=>'Saif'],
['firstname'=>'Syed','lastname'=>'Mujahid'],
['firstname'=>'Mohd','lastname'=>'Armaan']
];
}
}
And the view custom.php already exists in the Views folder.
But when I load the url http://localhost/ci4/public/index.php/test I get 404 Not Found error message.
Also I tried http://localhost/ci4/public/index.php/test/index but shows the same message.
So how to load this method from the custom controller class in Codeigniter v4 properly?
Except you're not parsing the $data to the view (you should do that adding a second parameter to view('custom', $data)), the code doesn't seem to be the problem: When a view could not be loaded in CI, it shows a specific error message (CodeIgniter\View\Exceptions\ViewException), not a 404 Not Found message.
Probably the problem is in another part of your project.
Try this
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return $this->load->view('custom',$data);
}
}
add the $data array to your views

Use a Facade outside Laravel 5 (in CodeIgniter)

I am trying to get a Laravel package (maatwebsite/excel) to work on CodeIgniter.
The error I get is: Message: A facade root has not been set.
Is there a way to get Laravel facades to work under CodeIgniter?
I read this article: https://www.sitepoint.com/how-laravel-facades-work-and-how-to-use-them-elsewhere/ which suggests to me that I need to call
Illuminate\Support\Facade::setFacadeApplication($app);
But what should $app be?
My code so far is this:
// Report.php
class Rapportage extends MY_Controller {
public function __construct()
{
parent::__construct();
// This doesn't work, what should I put here?
Illumiante\Support\Facade::setFacadeApplication($app);
}
public function generate_rapport()
{
// This is where the error occurs
return Excel::download(new PersonExport, 'rapport.xlsx');
}
}
// Reports/Persons.php
namespace Exports;
use Person;
use Maatwebsite\Excel\Concerns\FromCollection;
class PersonExport implements FromCollection
{
public function collection()
{
return Person::all();
}
}

CI_Model not found in extending class

I can't instantiate my models manually on my controller using require_once, following is the problem:
Controller
<?php
require_once './application/models/portion/portioncreatormodel.php';
class Payment extends CI_Controller {
function sample() {
$pc = new PortionCreatorModel();
echo 'OK';
}
}
Model
<?php
class PortionCreatorModel extends CI_Model {
function __construct() {
parent::construct();
}
}
When I load this model using $this->load->model('portion/portionCreatorModel') it works perfectly (in controllers or other models), when I load using require_once using this very same require_once './application/models/portion/portioncreatormodel'; it works perfectly in other models.
Why doesn't it work properly on controllers? I found this in my error.log:
PHP Fatal error: Class 'CI_Model' not found in /var/www/html/myerp/igniter/application/models/portion/portioncreatormodel.php on line 3
Note: I already tried this require_once:
require_once APPPATH.'/models/portion/portioncreatormodel.php';
Please, don't tell me the only way to solve it is renaming PortionCreatorModel to Portioncreator_Model, it is already working properly on models, the problem is on controllers.
Any glues?
Try using this in your controller:
<?php
class Payment extends CI_Controller {
function sample() {
$this->load->model('PortionCreatorModel', 'pc');
// Reference by using $this->pc
echo 'OK';
}
}
Reference: CodeIgniter: Models

Access to a different Controller

I want to access to my TestControler in DefaultController. So I've create a new instance, but the container is null. If I want to call a method, symfony throws a FatalErrorException:
Error: Call to a member function get() on a non-object in
DefaultController:
/**
* DefaultController.
*
*/
class DefaultControllerextends Controller
{
public function indexAction()
{
$contrTest = new TestController();
var_dump($contrTest);
}
var_dump result:
object(test\testBundle\Controller\TestController)#283 (1) {
["container":protected]=> NULL }
How can i do that?
Using other controllers inside a controller is a sign of bad architecture. Usually, it means you have to split the controller into a service, which you can use everywhere, and a controller.
For instance, when you have a controller which has a parseAction which parses a file and you need to use that in another controller too, you must create a acme_demo.parser.the_file_type service (give it which name you want) and use that in both controllers:
// ...
class FirstController extends Controller
{
public function xxxAction()
{
$parser = $this->get('acme_demo.parser.the_file_type');
$data = $parser->parse(...);
}
}
// ...
class SecondController extends Controller
{
public function yyyAction()
{
$parser = $this->get('acme_demo.parser.the_file_type');
$data = $parser->parse(...);
}
}

Yii: renderPartial can't load view when using CAction

I am trying to use the renderPartial method within a action class.
The method is unable to find the view.
The view is within the root of the app /views/invoice/view.php
<?php
class InvoiceAction extends CAction {
public $invoice_id = 0;
public function run() {
Yii::app()->controller->renderPartial('/invoice/view');
}
}
Switching to the following code will work fine:
Yii::app()->controller->renderPartial('//invoice/view');
Switched to the following code and it worked :
Yii::app()->controller->renderPartial('application.views.invoice.view');

Categories