Possible redirect function without route on Laravel?

Asked

Viewed 124 times

0

I’m using a feature to generate PDF reports using Laravel. However I am in doubt if it is possible from a simple HTML button, to redirect automatically to a function of mine controller without having to create a new route. I would like as an example to put the function and transform it into an object to be called in the view as shown below.

<a href="{{generatePDF()}}" class="btn btn-sm btn-primary">Gerar PDF</a>

How it would happen in Javascript. The function I wish to redirect is this:

function generatePDF(){
            $pdf = PDF::loadView('dashboard-pdf',$dados)->setPaper('a4', 'landscape');
            return $pdf->stream();
        }

Would it be possible to do something similar to Laravel or would even have to create a new route just to perform this function?

1 answer

1


What can be done is you create a Helpers folder in your app, create a file with its function and load it through the Composer, inserting in Composer.json, in "autoload" (as an example):

"autoload": {
    "psr-4": {
        "Intranet\\": "app/"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files": [
        "app/Helpers/ArquivoComFuncoes.php"
    ]

This way your function will be available for the entire application.

However, I see no reason not to make use of the routes (which is within the best practices).

  • It is that I have a large code to add the correct values in $data. And then I thought of some way to reduce that amount of code a little bit. But if you do that, that’s bad practice. I intend to create normal routes anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.