I placed the 'dompdf' # '/system/libraries/', and create a class file 'Dompdf.php' at the same dir, code as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
class CI_Dompdf extends Dompdf {
/**
* Set the template from the table config file if it exists
*
* #param array $config (default: array())
* #return void
*/
public function __construct($config = array())
{
log_message('info', 'Dompdf Class Initialized');
}
}
below is part of the controller function which using the dompdf library:
public function pdf_create($html, $filename='', $stream=TRUE){
$this->load->library('Dompdf','dompdf');
$this->dompdf->load_html($html);
$this->dompdf->render();
if ($stream) {
$this->dompdf->stream($filename.".pdf");
} else {
return $this->dompdf->output();
}
}
but I get the error below:
PHP Fatal error: Call to a member function isHtml5ParserEnabled() on null in /home/mysite/public_html/ci/system/libraries/dompdf/src/Dompdf.php on line 492
I'd checked the function does exists in 'dompdf/src/Options.php'.
I have no idea how to solve it.
My dev env:
Codeigniter 3.0.6
Dompdf 0.7.0
Thanks.
UPDATE Found error occurred due to not call Dompdf constructor, correct code as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
class CI_Dompdf extends Dompdf {
/**
*
* #param array $config (default: array())
* #return void
*/
public function __construct($config = array()) {
parent::__construct($config);
log_message('info', 'Dompdf Class Initialized');
}
}
Related
I can't use binance api in laravel
I installed Php binance api from composer require "composer require jaggedsoft/php-binance-api" from "https://github.com/jaggedsoft/php-binance-api" but examples not working in laravel.I had some errors when I tried.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Binance;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$api = new Binance\API();
$key = "----";
$secret = "---";
$api = new Binance\API($key, $secret);
$data = $api->price("BNBBTC");
return view('home', $data);
}
}
When I runned the route I got this error:
Error
Class 'App\Http\Controllers\Binance\API' not found
http://127.0.0.1:8000/home
Either import the Binance on top of your file or use backslash \
I have a very simple example to show the problem:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class VendorCounts extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'vendor:counts
{year : The year of vendor counts}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Runs vendor counts';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->info('Starting Vendor Counts');
}
}
<?php
namespace Tests\Feature\Console\Vendor;
use Tests\TestCase;
class VendorCountsTest extends TestCase {
public function testVendorCounts()
{
$this->artisan('vendor:counts', ['year' => 2019])
->expectsOutput('Starting Vendor Counts')
->assertExitCode(0);
}
}
I get the following error:
1) Tests\Feature\Console\Vendor\VendorCountsTest::testVendorCounts
Error: Call to a member function expectsOutput() on integer
/Users/albertski/Sites/vrs/tests/Feature/Console/Vendor/VendorCountsTest.php:12
I know the command definitely runs because if I put a dump statement in it shows the debug output.
I am using Laravel 6.3. Is there a different way to test this?
The problem I was using was that TestCase was using Laravel\BrowserKitTesting\TestCase as BaseTestCase. I ended up creating another Base just for console commands.
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class ConsoleTestCase extends BaseTestCase
{
use CreatesApplication;
}
Can you add this to your VendorCountsTest class:
public $mockConsoleOutput = true;
This is set by a trait but just making sure something hasn't changed the value. When $mockConsoleOutput is false it will directly run the artisan commmand. When it is true it will wrap it in a PendingCommand object that has those methods you are trying to call.
I had an issue where the use of expectedOutput() on my Artisan class would fail all the time, which turned out to be because I had used exit() and/or die() in a method, which really did not work well with phpunit test methods.
So if you want to stop processing the "script" at some point, just use an empty return and not exit() or die() if you want to utilize the built-in ->artisan() testing in Laravel.
Working example:
<?php
// app/Console/Commands/FooCommand.php
public function handle()
{
$file = $this->argument('file');
if (! file_exists($file)) {
$this->line('Error! File does not exist!');
return;
}
}
// tests/Feature/FooCommandTest.php
public function testFoo() {
$this->artisan('foo', ['file' => 'foo.txt'])->expectsOutput('Something');
}
Non-working example:
<?php
// app/Console/Commands/FooCommand.php
public function handle()
{
$file = $this->argument('file');
if (! file_exists($file)) {
$this->line('Error! File does not exist!');
exit;
}
}
// tests/Feature/FooCommandTest.php
public function testFoo() {
$this->artisan('foo', ['file' => 'foo.txt'])->expectsOutput('Something');
}
I have a small websockets chat written, the php part is just 2 files, server.php and Chat.php, they are both inside a bin folder and depend on ratchet and some other libraries which I downloaded to the laravel installation via composer.
server.php
require __DIR__.'/../vendor/autoload.php';
require 'Chat.php';
use Ratchet\Server\IoServer;
use Ratchet\http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080);
$server->run();
Chat.php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
foreach ($this->clients as $client)
{
if ($client !== $conn ) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'the following error occured: ' . $e->getMessage();
$conn->close();
}
}
Now, I have that bin folder inside the laravel root, and so I am able to start the server since the server.php is looking for dependencies in vendor one level up, but what I wanna do is use all the laravel goodies within these files, especially within Chat.php.
So now for example if I write use DB in Chat.php it gives an error (which I understand, it has no way of knowing laravel), so my question is how do I include this bin folder and its files so that I can use all the laravel goodies within them?
You do not need to manually load vendor/autoload.php because laravel does that for you.
First you have to create folder inside your YourLaravelRoot/app dir(Let's name that as Services). Then move chat.php into that, rename it to ChatService.php(Change class name also to ChatService) or any appropriate name(reccomanded to ends with xxxxService so it's easier to identify) and namespace it as namespace App\Services;(Assumming that your app name is App).Namespacing correctly is important otherwise you have to manually loads it throught composer.json .Then create a artisan command and move content of server.php into handle method inside command(Let's name it ServerCommand.php). Add use App\Services\ChatService as Chat;. Register the command in Kernal.php on app/console That's it. Now you should be able to access any laravel facade inside ChatService
Summary:
YourLaravelProject
-app
--Console
Kernal.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\ServerCommand::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
}
---Commands
----ServerCommand.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Services\ChatService as Chat;
class ServerCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'server:run';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080);
$server->run();
}
}
--Services
---ChatService.php
<?php
namespace App\Services;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
/**
*
*/
class ChatService implements MessageComponentInterface {
{
protected $clients;
function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
foreach ($this->clients as $client)
{
if ($client !== $conn ) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'the following error occured: ' . $e->getMessage();
$conn->close();
}
}
Execute command php artisan server:run
I'm using below code for my site, but it displayed me
404: Page not found.
routes
$route['class_name/function_name/(:num)'] = 'class_name/function_name/$1';
Controller.
public function function_name($Id)
{
print_r($Id); exit;
}
Use URI to extract the id from URL:
Following is the link in docs : http://www.codeigniter.com/userguide2/libraries/uri.html
Example- How to get the a URL using uri_string() codeigniter?
I have create a simple one maybe it could help you out
this is my controller welcome.php fresh from codeigniter
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
public function test1($ID) {
echo $ID;
}
public function test2($ID, $ID2) {
var_dump($ID, $ID2);
}
public function test() {
echo 'test';
}
}
and here is my route
$route['default_controller'] = 'welcome';
$route['welcome/test1/(:num)'] = 'welcome/test1/$1';
$route['welcome/test/(:num)/(:num)'] = 'welcome/test/$1/$2';
example to access it if working
https://192.168.248.209/stackoverflow/welcome/test2/1/3
https://192.168.248.209/stackoverflow/welcome/test1/1
this one is dynamic according to your server implementation
See Pretty Url Setup CodeIgniter
I am starting out with codeigniter, but the documentation is horribly written and doesn't actually work with the new version. My issue is that I cannot call a function within a model.
Here is my model: User.php
<?php
class User extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function test($x)
{
return $x;
}
?>
And my controller: welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->model('User');
echo $this->User->test('darn');
$this->load->view('welcome_message');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>
Check your {}s, your test function is outside your User class. Move it inside the class, then $this->User->test() will work.
<?php
class User extends CI_Model {
function __construct()
{
parent::__construct();
}
function test($x)
{
return $x;
}
}
?>
There's nothing "horrible" about the documentation.