Call Action from Controller with script

Is there a way to only call an action function of a controller via script and not via the direct URL?

For example, a page that contains content that should not be directly accessible.

You can do so via createControllerByID() method.

Below is an e.g.:

    $somecontroller = Yii::$app->createControllerByID('somecontroller');
    $someoutput = $somecontroller->somefunction($someParameter);

I would abstract the action vs. business logic into two functions so you call the business logic (in the example above that would be somefunction) as the action generally will do things like render pages that you may not require in your script. Also the controller function needs to be a public method to be called by controller id.

Lastly, see if this function can be moved to the related model. If the function can stand alone within the context of a model, it may be ideal to do that.

Thank you very much. I will look at this.

I have described my current problem here.

I would like to call this action to offer a PDF document for download after the user has clicked on a button.

If I enter the action directly via the url e.g. “site/downloadpdf” calls it works. But I don’t want this page to be accessible from the outside.

So I tried to call the function via Ajax. However, I then have no output.