I have templates folder in my public folder.
I want to have opportunity to upload different layouts form my admin panel and put them to public/templates folder.
How can I change standard layout then?
$template = 'christmas.phtml';
$viewModel = new ViewModel();
$viewModel->setTemplate('/../../public/templates/'.$template);
isn't working :(
I also tried this way (changing layout) up from current:
$this->layout('../../public/templates/'.$template);
I found the solution. I've added one line to my module.config.php:
'view_manager' => array(
...
'template_path_stack' => array(
__DIR__ . '/../view',
__DIR__ . '/../../../public' // newLine
),
),
Then simply use this in controller:
$template = 'sometemplate.phtml';
$this->layout('templates/'.$template);
can you try $viewModel->setTemplate(APPLICATION_PATH .'/../public/templates/'.$template); or $viewModel->setTemplate('./../../public/templates/'.$template); instead of $viewModel->setTemplate('/../../public/templates/'.$template);. Because I guess you want to go up from the application directory, not from the root directory and $template = 'christmas.phtml'; should be $template = 'christmas';
If there is no APPLICATION_PATH constant you can define it in index.php file. If you dont like to define it, you can use realpath('<path/to/your/public/templates/>') method.
Related
I am trying to get a simple module working. All it does is echo 'test' to a web page but I cannot get it working.
The first thing I did was add the module to app/Config/Autoload.php as follows;
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
'Modules\Filemanager' => ROOTPATH . 'modules'
];
Then, I created the following directory structure;
In Modules/Filemanager/Config/Routes.php I have added following route;
<?php
$routes->add('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index');
Lastly, in Modules/Filemanager/Controllers/Filemanager I have the following method:
<?php
namespace Modules\Filemanager\Controllers;
use App\Controllers\BaseController;
class Filemanager extends \App\Controllers\BaseController
{
public function index(){
echo 'test'; die();
}
}
When I go to my browser and enter example.com/filemanager/index I get the following error;
Controller or its method is not found: \App\Controllers\Filemanager::index
Any ideas would be much appreciated.
You've done few things wrong here.
In the App/Config/Autoload.php,
you've written 'Modules\Filemanager' => ROOTPATH . 'modules'
But in your directory structure image, there is no directory with the name 'modules'
Please change the 'Modules' folder name to 'modules'
Change 'Modules\Filemanager' => ROOTPATH . 'modules' to
'Modules' => ROOTPATH . 'modules' in App/Config/Autoload.php
In the Modules/Filemanager/Config/Routes.php
You're using the $routes variable in the below line
$routes->add('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index'); But $routes is not defined anywhere in the file.
Put
if(!isset($routes)) {
$routes = App\Config\Services::routes(true);
}
$routes->get('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index');
It should be $routes->get() instead of $routes->add()
Module level routing will not work until you add these routes in the App\Config\Routes.php file.
To add your modules routes to the app\Config\Routes.php
foreach(glob(ROOTPATH . 'modules/*', GLOB_ONLYDIR) as $item_dir) {
if(file_exists($item_dir . '/Config/Routes.php')) {
require_once($item_dir . '/Config/Routes.php');
}
}
After these changes your code should work.
I can easily render a template with variables from a file with some code like:
$renderer = new PhpRenderer();
$vm = new ViewModel();
$resolver = new TemplateMapResolver();
$resolver->setMap($this->templateMap);
$renderer->setResolver($resolver);
// Set the template to use and pass in variables as you normally would a view
$vm->setTemplate($template);
if ($vars) {
$vm->setVariables($vars);
}
$content = $renderer->render($vm);
I am curious how I can provide a string to setTemplate rather than a path to a template file. This way, the content being passed in can come from various sources such as an administrator's panel or database.
In the configuration example in the documentation you can see that it is possible to define a template_map inside your view_manager config array. A template map is like an array of aliases for your template files. So inside your module.config.php:
'view_manager' => array(
//...
'template_map' => array(
'name_from_admin_panel' => __DIR__ . '/../view/layout/view.phtml',
'name_from_database' => __DIR__ . '/../view/layout/view.phtml',
)
//...
)
Now you can use these names from your template map to set the template in your ViewModel as normally:
$template = 'name_from_admin_panel';
$viewModel->setTemplate($template);
Hello all im trying to load multiple libraries which is in different folders in library folder using namespaces but i keep getting not found
My directory structure is like this
app/
controllers/
models/
library/
views/
My loader.php is like this
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerNamespaces(array(
'Test\Name' => __DIR__ . "/../library/",
));
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
)->register();
And my basecontroller is trying to call like this
$var = new Test\Name\functions();
and btw the file functions in library is like this
class functions extends Phalcon\Mvc\User\Component
{
public function __construct()
{
}
public function initialize()
{
}
public function checking(){
echo 'checks';
}
}
i Keep getting
Fatal error: Class 'Test\Name\functions' not found in C:\wamp\www\app\controllers\ControllerBase.php on line 38
Any help is appreciated guys thnx
I think that your class should have:
namespace Test\Name;
class functions extends Phalcon\Mvc\User\Component
{
// ... rest of it
on top.
I would also make this configuration:
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
__DIR__ . "/../library/",
)
)->register();
So your class would be in (also I would rename your class to Functions:
app/library/Test/Name/Functions.php
So it would be obvious that your Functions class is in Test\Name namespace.
Having trouble finding the discussion that clued me in but, my understanding is that when using namespaces, you use namespaces. When using directories and other rules, you don't use namespaces.
Namespaces are faster so probably best to just stick with them, dropping the registerDirs as they are superfluous and mean the same thing as the namespaces:
library\Test\Name.php
becomes:
$loader->registerNamespaces(array(
'Apps\Module\Controllers' => $config->application->controllersDir,
'Apps\Module\Models' => $config->application->modelsDir,
'Test' => __DIR__ . "/../library/Test",
));
Then available as Test\Name.
$loader->registerNamespaces(array(
'App' => __DIR__ . "/../library/",
), true);
it will merge all sub dir, if you librarry dir architecture like this
library
- Test
- Name.php
You can call new \App\Test\Name();
i'm new to zend framework 2 and have stumbled on this problem that i can't find a answer to:
i have a layout.phtml and i want before the layout loads to have a (layout) controller fetch from my db some junk and pass it the the layout to render, so no matter if it is my application module or any other module that is running, the layout will always use the same controller.
my modules are:
--module
--application
--...
--src
--application
--controller
LayoutController.php <-- where i would like to hold my layout controller
--view
-- ...
--layout
layout.phtml <-- where i hold my layout
--shop
-- ... <-- shop module that uses the application's layout
and my module.config.php layout is defined:
'view_manager' => array(
....
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
thanks!
If you want to fetch some data from database and pass to layout, try this in your Module.php:
public function onBootstrap(MvcEvent $e)
{
//...
$application = $e->getApplication();
$sm = $application->getServiceManager();
$application->getEventManager()->getSharedManager()
->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch',
function($e) use ($sm) {
$dbResult = $sm->get('YourModule\Model\FooTable')->bar();
$sm->get('ControllerPluginManager')->getController()->layout()->dbResult = $dbResult;
}
, 2
);
//...
}
And in your layout.phtml use $this->dbResult for your database result.
I'm new to Zend, so pls help me out on this one, since I have searched a lot and tried a lot, I really did.
here is the very old question:
this is my current url:
www.sample.com/blog/detail/index/id/5
wanted:
www.sample.com/url-rewrite-tips
well, I put following code in the bootstrap.php
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(
'url-rewrite-tips',
array(
'module' => 'blog',
'controller' => 'detail',
'action' => 'index',
'id' => '5'
)
);
$router->addRoute('url-rewrite-tips', $route);
it was working, but it also is a hardcode. i tried to get param in bootstrap.php, but failed.
Now, I have more than 100 ids. However, I tried to put it in index.pthml, in a foreach(){} , then it fails.
I have rewrite names in the DB for every article, how am I supposed to it?
better not use configs or .htaccess
Thanks In advanced.
What I always do is I add the routes.ini execution in bootstrap:
protected function _initRoutes()
{
$this->bootstrap('FrontController');
$front = $this->getResource('FrontController');
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'production');
$router = $front->getRouter();
$router->addConfig($config, 'routes');
}
Then in routes.ini:
[production]
routes.content.route = "/s/:permalink"
routes.content.defaults.controller = "content"
routes.content.defaults.action = "show"
routes.content.reqs.permalink = "[a-z0-9-]+"
So now when someone access http://mydomain.com/s/some-nice-permalink routing goes to ContentController ShowAction() method, which takes permalink as param and finds it in database.
Of course you'll have to also write methods for creating unique permalinks in database.