Symfony layout in another folder - php

Is it possibile to put layout files in the modules/templates (or something else) directory instead of the general app/templates?
I've tried to put the path in view.yml but it doesn't work.

Just found a workaround so I can keep layout.php inside the module/templates folder:
public function preExecute()
{
$template = $this->getContext()->getConfiguration()->getTemplateDir('MODULE', 'layout.php');
$this->setLayout($template . '/layout');
}

Related

Create for each hook function a file in WHMCS modules

In WHMCS can i use a folder for hooks inside of hooks.php in modules/addons/ like in includes/hooks?. I test it but it don't work. and i tried to find solution by creating a folder named hooks and inside this folder i create files that contain functions: Exemple :
In modules/addons/myModule/hooks/function1.php :
function function1($vars){
// .....
}
In modules/addons/myModule/hooks.php :
if (!defined("WHMCS"))
die("This file cannot be accessed directly");
foreach (glob("hooks/*.php") as $filename)
{
include($filename);
}
add_hook("ClientAreaHeadOutput", 1, "function1.php");
My solution don't work. There is any best solution for that? Thanks.
To get an addon detected put your addon folder inside modules/addons/ e.g modules/addons/mymodule and then inside your module folder have a php file with the same name modules/addons/mymodule/mymodule.php. Your functions go into this file.
You will then go to Setup > Addon Modules interface and activate it.

Return html from controller action in Zend

i have a structure like so:
modules -> Controllers -> Amodule -> IndexController
there i have an action which is called:
public function getTemplateAction(){
... //1) load html file
//2) return html
}
this function is called from javascript with get via the url: baseUrl+'/Amodule/index/get-template/viewid/3.
I would like based on the viewid to return an html template, where should i put the .html file and how can i load it in Zend? i tried
this->partial('thetemplate.html');
and putting the html file in Amodule folder but didnt work.
Thanks in advance!
Added another folder at the same level as controller called it views/scripts and in there i created the partial. Then from the view i load the partial as:
<?php echo $this->partial('_partialName.phtml', array(
var1 => value1,
var2 => value2
/*... vars to pass in the partial*/
));?>
A simple solution is to have your html files in a dedicated folder at the root of your project and output the file with readfile :
public function getTemplateAction() {
readfile(APPLICATION_PATH . '/../templates/thetemplate.html');
exit;
}

Zend 2:: getting public folder path or basePath() easily in controller action

In my application, to move a file to a specific directory i need to know public folder path in controller action. I read different this type solution but not getting easy one. I know that in view we can get easily public folder path using $this->basePath(); view helper. I exactly want this in controller action. Anybody can guide me how can i achieve that. Thanks in advance.
index.php sets the current working dir to you application root (the folder containing composer.json, init_autoloader.php, etc.)
As long as you haven't called chdir elsewhere in your application you can call getcwd() and it'll always return the path to your app root.
Since the public folder is relative to that, you can get the path using ...
$publicDir = getcwd() . '/public';
In your public folder edit your file named index.php
add only two lines
define('BASE_PATH', realpath(dirname(__DIR__)));
define('PUBLIC_PATH', BASE_PATH.'/public');
you can use in your code like
print_r(BASE_PATH);
print_r(PUBLIC_PATH);
If you want to include a file from public folder (independence with location of index.php file):
include_one ("./public/your-file.php");
You should try this if you want the public folder:
$publicPath = $_SERVER['DOCUMENT_ROOT'];
Or try this if you want the basepath:
$basePath = dirname($_SERVER['DOCUMENT_ROOT']);
You could use view helpers from within a controller in ZF2 as it shown here and here. You may try this for your case :
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
$url = $renderer->basePath('the_ressource_you_want_to_get_from_public_folder');

define custom variables and direct call the variables in view part

What I want to do is creating a file like my_custom_settings.php in config directory and call the defined variable in view part.
let's say in my_custom_settings.php:
define('TEMPLATE_DIR', 'assets/front');
and in view part direct in HTML:
<link href="<?=TEMPLATE_DIR?>/stylesheet/style.css">
or any other alternative solution??
PS: Now I am using base_url() to access the path
personally i extend the /core/helpers/url_helper.php , defaults are site_url() , base_url(), current_url(); etc ... i just extended that for having base_static_url();
so put in core/helpers/url_helper.php:
if ( ! function_exists('base_static_url'))
{
function base_static_url()
{
$CI =& get_instance();
return $CI->config->slash_item('base_static_url');
}
}
then in config.php file you just add 1 more line:
$config['base_url'] = "http://mysite.com/";
$config['base_static_url'] = "http://mysite.com/static/"; //path to your static resources folder
then you can call static resources using :
<img src="<?php echo base_static_url();?>img/myimage.png"/>
ok this is might be more then what you are looking for,
but this is a way to put site wide configs in one file, and then easily have them available
in config folder you have file: my_custom_settings.php
in that file you want to set a config value like:
$config['TEMPLATE_DIR'] = 'assets/front' ;
$config['site_slogan'] = 'Laravel? Never heard of it' ;
create another file called: My_custom_settings.php
put that file in: application/library/My_custom_settings.php
that file will contain:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_custom_settings
{
function __construct($config = array() )
{
foreach ($config as $key => $value) {
$data[$key] = $value;
}
// makes it possible for CI to use the load method
$CI =& get_instance();
// load the config variables
$CI->load->vars($data);
}
} // end my custom settings
now in your controller constructor
public function __construct() {
parent::__construct();
// Load configs for controller and view
$this->load->library( 'my_custom_settings' );
$this->config->load( 'my_custom_settings' );
} // end construct
Now for the cool part -- anything you put in that config file, will be available for your controller and views. ( you can load config in a model constructor as well ).
in a controller or model you get the value with $this->config, like
$this->config->item( 'site_slogan' )
a little awkward, but for views, heres the reward, you only need the config name
echo $TEMPLATE_DIR . '/somefile' ;
Images, css, javascript, pdfs, xml... anything that is allowed to be accessed directly should not be living in your application directory. You can do it, but you really shouldn't. Create a new folder at the root of your directory for these files, they should not be mixed into your application, for example: in your views folder.
Chances are, you're using an .htaccess file, which is only allowing certain directories to be accessed via http. This is very good for security reasons, you want to stop any attempt to access your controllers and models directly. This is also why we check if BASEPATH is defined at the top of most files, and exit('No direct script access.') if not.
To get the correct path to these resources (js/css/images), you can't use relative paths, because we aren't using a normal directory structure. The url /users/login is not loading files from the directory /users/login, it probably doesn't even exist. These are just uri segments that the bootstrap uses to know which class, method, and params to use.
To get the correct path, either use a forward slash (assuming your app and assets are in the root directory, not a subdirectory) like this:
Or your best bet, use an absolute url:
// References your $config['base_url']
" />
Equivalent to:
http://mydomain.com/images/myimage.jpg
There are helpers built into CI that you can optionally use as well, but this is really all you need to know.

Simple File include problem in Zend?

I have created a zend project on ubuntu which is in /var/www/student/ directory.
Now I have head.phtml file in this location:
/student/application/views/scripts/index/head.phtml
When I try to include head.phtml file in
/student/application/modules/test/views/scripts/all/index.phtml
Like this:
echo $this->partial('index/head.phtml');
It gives me following error:
Message: script 'index/head.phtml' not found in path (/var/www/student/application/modules/notification/views/scripts/)
Including files is always a difficult job for me. How to fix this. I have to include this file in many modules then what is permanent solution for this that I should not guess the path
Thanks
You can add several path to look for script view files. The best way is to do it in the bootstrap file for all your common files (like head, footer, metas...).
Just add a setupView method in your bootstrap where you deal with everything which is realted to your views :
protected function _initView()
{
$view = new Zend_View();
// setup your view with jquery and other stuffs...
// [ ... ]
// add the view directory to the stack of scripts paths
$view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
}
<? $this->setScriptPath('/student/application/views/scriptss'); ?>
<?= $this->partial('index/head.phtml') ?>
I added following line in Controller's init() function.
public function init() {
$this->view->addScriptPath( APPLICATION_PATH . '/views/scripts/' );
}
Add head.phtml file in view like this:
echo $this->partial('index/head.phtml');
File is successfully added.

Categories