PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 20, 2022

[FIXED] How to use a composer package in CodeIgniter 4?

 May 20, 2022     autoload, codeigniter, codeigniter-4, composer-php, php     No comments   

Issue

I find many solutions for CI3 on the web, but none works for CI4.

I installed a package using composer

composer require chillerlan/php-qrcode

Then I added a new Controller

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class Qr extends BaseController
{
    public function index($path)
    {
        $data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';

        echo '<img src="' . (new QRCode)->render($data) . '" alt="QR Code" />';
    }
}

This gives me Undefined type 'App\Controllers\QRCode'. for the new QRCode.

What do I need to do, to use the QRCode class?


Solution

Try this

<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use chillerlan\QRCode\{QROptions, QRCode};

class Qr extends BaseController
{
    public function index($path)
    {
        $data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';

        echo '<img src="' . (new QRCode)->render($data) . '" alt="QR Code" />';
    }
}


Answered By - WiatroBosy
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing