Use a Facade outside Laravel 5 (in CodeIgniter) - php

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();
}
}

Related

using traits inside laravel controller

I have some repetitive codes inside my laravel(7.23.0)controller
use App\ModelA;
use App\ModelB;
use App\ModelC;
use App\Traits\DbTrait;
class DarsController extends Controller
{
use DbTrait;
public function A($id) {
return ModelA::where('column', $id)->get(*);
}
public function B($id) {
return ModelB::where('column', $id)->get(*);
}
public function C($id){
return ModelC::where('column', $id)->get(*);
}
//the only difference in these codes is model, all codes are the same
}
I had created a folder named Traits and inside that I had defined a trait DbTrait.php
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName , $id){
return $ModelName::where('column', $id)->get('*');
}
}
so I modified my controller's functions to this
public function A($id) {
// return ModelA::where('column', $id)->get(*); works fine
$this->getAllz('ModelA', $id);// throws an error
}
it throws an error message: "Class 'ModelA' not found"
thank you
update:
i should apologize, i am really sorry, 3 of the answer worked, and i see the data inside network tab,i am using vue to display data,
and i think using trait made a complex array
this is my simple vue
axios.get('/api/emla/' + id).then(response =>{
this.data = JSON.parse(JSON.stringify(response.data));
}
Import the classes which will be used in your trait as you have done in the controller class.
use App\ModelA;
use App\ModelB;
use App\ModelC;
the reason you're getting Class 'ModelA' not found is because it's looking for that class in your traits directory, which it won't find. so you need this instead:
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName , $id) {
return app( "\App\\" . $ModelName )::where('column', $id)->get('*');
}
}
assuming that you've defined your models under \App namespace
you need to modify your trait
ModelName::where it try to load the class in trait so use $ModelName->where here $ModelName is already instance of that class so you can call function via -> operator
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName ,$columnName ,$id){
return $ModelName->where($columnName, $id)->get('*');
}
}
and to call this function
$this->getAllz(new ModelA, $id);
this way you don't need to import class inside trait
your final code will be like this
use App\ModelA;
use App\ModelB;
use App\ModelC;
use App\Traits\DbTrait;
class DarsController extends Controller
{
use DbTrait;
public function A($id)
{
return $this->getAllz(new ModelA,'column', $id);
}
public function B($id)
{
return $this->getAllz(new ModelB,'column', $id);
}
public function C($id)
{
return $this->getAllz(new ModelC,'column', $id);
}
}
100% should work if you follow this
public function A($id) {
// return ModelA::where('column', $id)->get(*); works fine
$model = new ModelName(); // new ModelName;
$this->getAllz($model, $id);// throws an error
}
public function getAllz($ModelName , $id){
return $ModelName->where('column', $id)->get('*');
}

Symfony2: How to call PHP function from controller?

I need use a PHP function to convert numbers to letters. I create a file named: converter.php in the folder “Controller”. I need call the function named “convertir_numero_letras($number)” insert in converter.php.
I call the function “convertir_numero_letras($number)” from the following controller:
public function convertirAction()
{
$number=1234;
$this->convertir_numero_letras($number);
return $this->render('contratos/mostrar_cifra.html.twig', array('numero_convertido' => $numero_convertido));
}
But this code not work. I obtain the following
error message:
Attempted to call an undefined method named "convertir_numero_letras" of class "BackendBundle\Controller\ContratosController".
As I can fix this?
you can create a folder inside bundle like yourBundel/Helper
and create you custom class in folder like ConverHelper.php
like this :
class ConvertHelper
{
public static function numero_letras($param)
{
return 'converted value';
}
}
and then you can call in any controller and any bundle like this:
yourBundle\helper\ConverterHelper::numero_letras('someting');
don't forget to add namespace of ConverterHelper to your contoller file
example:
// file: GRF/BlogBundle/Helper/ConverterHelper.php
<?php
namespace GRF\BlogBundle\Helper;
class ConverterHelper
{
public static function toNum($param)
{
return $param;
}
}
and usage in controller:
//file controller
<?php
namespace GRF\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PostController extends Controller
{
public function indexAction()
{
return new Response(\GRF\BlogBundle\Helper\ConverterHelper::toNum(32434));
}
}

Call to undefined method Illuminate\Database\Query\Builder::groups() eager loading

I am creating a website using laravel. I have a small issue with eager loading. I have already made several websites with laravel, but still I can't find what is wrong here.
This is my config model:
<?php
class Config extends \Eloquent {
protected $table = "configs";
public function groups() {
return $this->hasMany('ConfigOptionGroup', 'config_id');
}
}
And this is my testcontroller class:
<?php
namespace WebsiteController;
class DemoController extends \BaseController {
public function getTest() {
$c = \Config::where('id', 1)->with(['groups' => function($q){
$q->whereNull('config_option_group_id');
}])->first();
return $c;
}
}
Whenever I surf to the url that calls the getTest method I get an error saying Call to undefined method Illuminate\Database\Query\Builder::groups(). However, the function groups() exists in the Config model.
When I remove the with() function from the query, it works just fine. But I can never load the groups via the relation.
Is there anyone who can help me with this problem?
Update: I have removed the Config facade by commenting out the line in /app/config/app.php.

Issue with Facade and injected dependency in Laravel 4

I am having an issue getting a Facade to work properly with a dependency injected into the underlying class.
I have a class called 'Listing'. It has one dependency called 'AdvertRepository' which is an interface and a class called EloquentAdvert which implements the interface. The code for these three classes is here:
// PlaneSaleing\Providers\Listing.php
<?php namespace PlaneSaleing\Providers;
use PlaneSaleing\Repositories\Advert\AdvertRepository;
class Listing {
protected $advert;
public function __construct (AdvertRepository $advert_repository) {
$this->advert = $advert_repository;
}
public function test() {
$this->advert->test();
}
public function test2() {
echo "this has worked";
}
}
// PlaneSaleing\Repositories\Advert\AdvertRepository.php
<?php namespace PlaneSaleing\Repositories\Advert;
interface AdvertRepository {
public function test();
}
// PlaneSaleing\Repositories\Advert\EloquentAdvert.php;
<?php namespace PlaneSaleing\Repositories\Advert;
class EloquentAdvert implements AdvertRepository {
public function test() {
echo 'this has worked';
}
}
I have then created a service provider called ListingServiceProvider.php, which has the following code:
// PlaneSaleing/Providers/ListingServiceProvider.php
<?php namespace PlaneSaleing\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ListingServiceProvider extends ServiceProvider {
public function register() {
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', 'PlaneSaleing\Repositories\Advert\EloquentAdvert');
}
}
I also added this to the ServiceProviders array in app.php
Now, if I inject Listing as a dependency into a controller and call the test method (as shown below) Laravel correctly detects the dependency, instantiates EloquentAdvert via its binding and displays 'this has worked'.
// Controllers/TestController.php
use PlaneSaleing\Providers\Listing;
class TestController extends BaseController {
protected $listing;
public function __construct(Listing $listing) {
$this->listing = $listing;
}
public function test1() {
$this->listing->test();
}
}
Now, I then created a facade for Listing. I added a new facade as follows and added an alias in app.php:
// PlaneSaleing\Providers\ListingFacade.php
<?php namespace PlaneSaleing\Providers;
use Illuminate\Support\Facades\Facade;
class ListingFacade extends Facade {
protected static function getFacadeAccessor() {
return 'Listing';
}
}
I also added the following new lines to ListingServiceProvider.php:
<?php namespace PlaneSaleing\Providers;
use PlaneSaleing\Repositories\Advert\AdvertRepository;
use PlaneSaleing\Repositories\Advert\EloquentAdvert;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ListingServiceProvider extends ServiceProvider {
public function register() {
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', 'PlaneSaleing\Repositories\Advert\EloquentAdvert');
// New lines...
$this->app['Listing'] = $this->app->share(function() {
return new Listing(new AdvertRepository);
});
}
}
NOW...if I call Listing::test(), I get the following error: Cannot instantiate interface PlaneSaleing\Repositories\Advert\AdvertRepository.
If I call Listing::test2() , I get 'this has worked' so it seems the Facade is working correctly.
It seems that when accessing Listing via its Facade the binding between AdvertRepository and EloquentAdvert doesnt work. I have looked at my code in the ServiceProvider thinking it was the issue, but I cant figure it out.
Both the Facade and binding work when tested individually but not when both are used at the same time.
Any ideas???
OK, So I have figured it out...For those who run into a similar problem...
The offending statement was in ListingServiceProvider.php which read:
$this->app['Listing'] = $this->app->share(function() {
return new Listing(new AdvertRepository);
});
The error is the new AdvertRepository statement. The reason being is that, we are telling php to directly instantiate the interface 'AdvertRepository'. Instead, we need to tell Laravel to instantiate the appropriate implementation of the 'AdvertRepository' interface. To do that, we use App::make('AdvertRepository'). That way, Laravel uses the binding previously declared to instantiate the correct implementation.
If your constructor is not being inject with a class, you must tell Laravel what class will be used when it needs to instantiate a particular interface:
Put this in your filters or bindings file:
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', function()
{
return new PlaneSaleing\Repositories\Advert\EloquentAdvert;
});

What is the correct way to access a model in an controller on Laravel

I'm new on Laravel 4 and I am trying to understand it.
is searched on google and on stackoverflow. Maybe i am not searching for the right syntax but i hope someone can help me, with it.
In CodeIgniter i understand it (probably). There I use in an Controller:
function __construct()
{ $this->load->model('example_m'); }
But how about in Laravel 4?
I figured out the following:
i make a static function in de model so i can access it everywhere. Example:
class Example extends Eloquent // this is the model
{
public static function TestExample(){
// do some stuff here
}
}
Or i could do it like this:
class ExampleController extends BaseController
{
public $test = null;
public function __construct()
{
$this->test = new Example();
}
public function index()
{
$this->test->TestExample();
}
}
My question is: Is there an other way and/or what is the correct way?
http://four.laravel.com/docs/ioc
App::bind('ExampleModelInterface', 'Example');
class ExampleController extends BaseController {
public function __construct(ExampleModelInterface $model)
{
$this->model = $model;
}
}
Do you mean simply accessing the method of a model?
Since they are static you use: Modell::method()
You might have to do a composer dump-autoload though so L4 autoloads it correctly.

Categories