Dompdf in services not found; Controller error "Message: Class 'Dompdf' not found" - twig

I am working with symphony and slim, so I use services that can be called in my controllers, then to my twig views. I installed Dompdf with composer and added it to my list of services I my bootstrap folder. I have tried several ways to call dompdf, but still get
Message: Class 'Dompdf' not found
This is the code in my controller:
class SlipController extends \App\Controllers\Base\PageController{
function getHandler($request, $response, $args)
{
// Instantiate Dompdf with our options
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream("mypdf.pdf", [
"Attachment" => true
]);
return $this->view->render($response,'pages/slips.twig');
}
In the services.php
<?php
$container['dompdf'] = function($container) {
return new \Dompdf\Dompdf;
};

Did you add a use statement to your code?
use Dompdf\Dompdf;
class SlipController extends \App\Controllers\Base\PageController{
function getHandler($request, $response, $args)
{
...

Related

Laravel export to excel - download issue

I can't generate xlsx file. I'am using maatwebsite/excel 3.1. I have installed it via this:
composer require maatwebsite/excel
I've added 'Excel' => Maatwebsite\Excel\Facades\Excel::class, to Aliases and Maatwebsite\Excel\ExcelServiceProvider::class, to providers.
I have structure of project like ProjectName->app->Exports.
<?php
namespace App\Exports;
use App\Order;
use App\Status;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class ActiveOrderExport implements FromCollection
{
public function collection()
{
return Status::all();
}
}
In repository I have function excel, also with these inclusions:
use App\Exports\ActiveOrderExport;
use Maatwebsite\Excel\Facades\Excel;
public function excel()
{
return Excel::download(new ActiveOrderExport(), 'activeOrders.xlsx');
}
Route:Route::get('/export_excel', 'HomeController#excel')->name('admin.export_excel.excel');
Home Controller:
{
$this->AOrepository->excel();
//return back();
}
My problem is that no error is shown, only blank page. No download is executed in the browser.
I've watched several tutorials, I haven't noticed any important diffrence among the codes.
Can you help me please?

Call LitElement method from outside the element

I have a simple LitElement component like so:
class MyElement extends LitElement {
constructor() {
super();
}
customMethod(data) {
// do something with the passed parameter
}
render() {
return html`<div id="element"></div>`;
}
}
customElements.define('my-element', MyElement);
And I want to be able to call that customMethod from outside of my element.
So for example if I add the element to web page like so:
<my-element></my-element>
I then want to be able to add some JavaScript to the page and call that customMethod.
I tried:
var element = document.getElementById('element');
element.shadowRoot.customMethod('example data');
But it claims it's not available... How can I call a method on an instance of LitElement?
You don't need to use shadowRoot in the call :
var element = document.getElementById('element');
element.customMethod('example data');
but you need to be able to locate your element
<my-element id='element'></my-element>
I had a very similar problem and the existing answers did not seem to fix it. The reason for my issue was caused by the fact that LIT Element scripts are exported as modules, meaning that they are loaded and executed after the initial DOM has been parsed. So if you are using a script to access the public method - make sure that it is also in a module (or you can alternatively place the code into an appropriate timeout).
So when defining an element in LIT Element as follows:
#customElement('my-element')
export class MyElement extends LitElement {
#state()
text = '';
customMethod(data) {
this.text = 'Custom method was called!';
}
render() {
return html`<div id="element">${this.text}</div>`;
}
}
And adding a script in my index.html page:
<my-element id='element'></my-element>
<script type="module">
const element = document.getElementById('element');
element.customMethod();
</script>
Make sure that the script tag contains type="module". Otherwise you will see the following error in the console: Uncaught TypeError: element.customMethod is not a function
Link to LIT Element Playground.
Also, here is a great article that explains how scripts are loaded in detail.

get file like pics and media through controller

I have the following link on my website - http://mywebsite/multimedia/pronounciation/265.mp3
which gets me the file bypassing controllers. But I would like to log request and then return this file. So I created controller which logs request and then reroutes to the file:
class Controller_GetSound extends Controller {
public function action_index() {
Request::factory('multimedia/pronounciation/265.mp3')
->method(Request::POST)
->post($this->request->post())
->execute();
}
}
But it doesn't work as expected. How can I return resource file from controller?
Kohana has a send_file function. That function does a good job on splitting large files and sending correct mime types.
#see http://kohanaframework.org/3.3/guide-api/Response#send_file
Your code should be:
class Controller_GetSound extends Controller {
public function action_index() {
$this->response->send_file('multimedia/pronounciation/265.mp3',TRUE,array(
'mime_type' => 'audio/mpeg',
))
}
}
You actually don't really need to set the mime_type. Kohana will find the correct mime_type for your.
It sounds like you want to implement something known as X-Sendfile. I think?
The controller would look something like this:
class Controller_GetSound extends Controller {
public function action_index() {
$this->response->headers(array(
'Content-Type' => 'audio/mpeg'
'X-Sendfile' => 'multimedia/pronounciation/265.mp3',
);
}
}

Kohana - one function on every controller

I have a project in Kohana 3.3.
I have a lot of controllers, models etc.
Now, I want add one functionality - close the whole site for all users.
Where I can add function, which for example, will redirect users to http://mypage.com/website_is_close ?
Example:
function check(){
$isClose = DB::query(.....)
if($isClose) header("Location: http://mypage.com/website_is_close");
return false;
}
Thanks :)
In Controller_Base all other controllers extend from. E.g.
File application/classes/Controller/Base.php
class Controller_Base extends Controller_Template {
public function before()
{
$isClose = DB::query(.....)
if($isClose)
{
HTTP::redirect("http://mypage.com/website_is_close");
exit ;
}
parent::before();
}
}
All other classes should extend from that class e.g
class Controller_Home extends Controller_Base {}
I personally use this also for every subdirectory e.g.
// As all controllers in the user folder probably need to be supplied with a user anyway
class Controller_User_Base extends Controller_Base {}
class Controller_User_Profile extends Controller_User_Base {}
I think a better approach would be to add a "catch all" route to the beginning of your routes list.
It would catch all URLs and would point to a controller that you would create. This is far cleaner than hacking away at a base controller.
How does this look?
Route::set('closed', '(<url>)', array('url' => '.*'))
->defaults(array(
'controller' => 'Closed',
'action' => 'index',
));

Why can't Kohana find my controller?

I have the following controller:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Static extends Controller_DefaultTemplate {
public function action_index()
{
View::set_global('message', '<span class="highlight">This is a global message.</span>');
$data = array (
'siteTitle' => 'Kohana Test Site',
'siteSubtitle' => 'A site to learn Kohana',
'menu' => View::factory('blocks/menu'),
);
$view = View::factory('templates/layout', $data);
$this->request->response = $view->render();
}
}
but kohana gives me the error:
ErrorException [ Fatal Error ]: Class
'Controller_DefaultTemplate' not found
although Eclipse can find the file (via F3) and I thought Kohana was able to find all classes via autoloading?
How can I get Kohana to find the Controller_DefaultTemplate class so I can extend Controller_Static?
You must include file with definition of Controller_DefaultTemplate
The problem was that my file name defaultTemplate.php was camel case, changing it to all-lowercase defaultemplate.php enabled Kohana to find the class inside it.

Resources