Is there a helper method for getting the absolute path to storage directory of masonite? - masonite

I need to be able to get the absolute path in my controller and my views for a storage path. Some storage paths are different for different static assets in my Masonite application.
How can I do this?

Masonite has a helper for this. You can use it directly in your views like this:
Assuming a configuration like this:
DRIVERS = {
'disk: {
'location': '/storage/directory'
}
}
You can do this:
<img src="{{ static('disk', 'profile.jpg')">
this will result in an output like /storage/directory/profile.jpg
Now assuming you had a configuration file like this:
DRIVERS = {
'disk: {
'location': {
'storage': '/storage/directory',
'uploads': '/storage/uploads',
}
}
}
You can just slightly modify your static helper:
<img src="{{ static('disk.uploads', 'profile.jpg')">
You can also use the helper outside of views by using from masonite.helpers import static

Related

How to prevent models being generated with full namespace?

When I run openapi-generator-cli the Models and containing files are being output with their names containing the full namespace of the API classes. This is very awkward to read. I'd like the output to contain only the Model name, without the namespace.
E.g for class
namespace My.NameSpace.Common.V1.Models.Dto
{
public partial class PortfolioAsset
{
// properties
The output looks like
my-namespace-common-v1-models-dto-portfolio-asset.ts
With the content of the file as
export interface MyNamespaceCommonV1ModelsDtoPortfolioAsset {
// parameters
}
I just want the file to be called portfolio-asset.ts and the content to be
export interface PortfolioAsset {
// parameters
}
How can I do this?
The fix was simply to remove the CustomSchemaIds line, which specified that I should use the full name of each type being generated.
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = AppSettingsProvider.AppSettings.ApplicationName, Version = "v1" });
// c.CustomSchemaIds(x => x.FullName); <-- delete ftw
});

How to retrieve session values in Sightly/HTL?

I am using Sightly/HTL as the templating language in my AEM project (AEM version 6.3). As Sightly provides a lot of context objects, two of them being : request backed by org.apache.sling.api.SlingHttpServletRequest and currentSession backed by javax.servlet.http.HttpSession, I am trying to access some session parameter values in my sightly file by doing something like below:
${request.session.attribute # mySessionAttribute}
or
${currentSession.attribute # mySessionAttribute}
but am not able to get that value. Does anybody has any idea about how to do it?
In HTL/Sightly you cannot call arbitrary methods with parameters, it's a limitation by design. Since the javax.servlet.http.HttpSession API does not expose attributes as a map you can't access them as ${currentSession.attributes['mySessionAttribute']} so you will need to be creative about it:
script.html
<sly data-sly-use.attr="${'attrib.js' # session=currentSession, name='mySessionAttribute'}">${attr.value}</sly>
attrib.js
use(function () {
return {
value: this.session.getAttribute(this.name)
};
});
You can not pass arguments to methods in HTL like this and I would not recommend doing it anyway.
One way to solve this issue is to use a Sling Model:
#Model(adaptables = SlingHttpServletRequest.class)
public SessionModel {
#ScriptVariable
private Session currentSession;
public String getMySessionAttribute() {
return this.currentSession.getAttribute("attributeName");
}
}
HTL:
<div data-sly-use.sessionModel="com.mypackage.SessionModel">
${sessionModel.mySessionAttribute}
</div>

Scala Play 2.5 Controller class to serve static HTML

I want to serve a static file from a Scala Play controller. I am looking for something that would allow me to do something like this example below.
NOTE: This obviously does not work.
It is very possible that I am look at the problem in the wrong way, I however do NOT want to redirect to the app.html
def loadApplication(): EssentialAction = Action.sync { request =>
val contents = Assets.contentsOf("/public/assets/app.html") //This doesnot return the contents, but that is what I want
Ok(contents)
}
You can just use the Assets and return contents via that. You might have to tweak the path though:
class MyController #Inject() (assets: Assets) extends Controller {
def loadApplication(): Action[AnyContent] = Action.async { request =>
assets.at("/public/assets/", "app.html").apply(request)
}
}
More information may be found in the documentation: https://www.playframework.com/documentation/2.5.x/AssetsOverview#The-Assets-controller
Also note that you can map a route to your assets instead of statically referencing the file from the controller, like so:
GET /assets/*file controllers.Assets.at(path="/public", file)

External Php class in Yii?

Hello I need to use evalmath.class.php within a Yii application. How can I include it from my controller?
Something like this:
public function actionFormulas() {
include('models/evalmath.class.php');
$m = new EvalMath;
...
}
But the above code does not work. How can I include an external class within a controller?
In your example, to use include/require you probably need to add some path info with dirname(__FILE__).'/../models/...' or similar, but to do it within the Yii framework, you would first create an alias (usually in your main config file) with setPathOfAlias :
Yii::setPathOfAlias('evalmath', $evalmath_path);
Then you can use Yii::import like so:
Yii::import('evalmath', true);
and proceed as you were:
$m = new EvalMath();
..etc...
class ServiceController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder
echo ConsolidateController::test(); // test is action in ConsolidateController

Zend Framework: Get whole out output in postDispath while using Layout

I have a layout loader plugin which looks like this:
class Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
$config = Zend_Registry::get("config");
$module = $this->getRequest()->getModuleName();
if (isset($config->$module->resources->layout->layout) && !$this->getRequest()->format)
{
$layoutScript = $config->$module->resources->layout->layout;
$this->getActionController()->getHelper('layout')->setLayout($layoutScript);
}
}
}
In a controller plugin I then want to get the whole of the response like so
$this->getResponse()->getBody()
This however only returns the output from the action, not the output from the layout too.
How can I get the whole output, layout and action together?
Thanks!
I believe that Zend_Layout operates at postDispatch() with a high stack index. So, to get the content, you might need to do your access later, at dispatchLoopShutdown().