Map controller and view (Suit CRM 7.7.8) - php

I am very new to SugarCRM (SuitCRM 7.7.8). I could create a controller and could echo some strings in it. I wanted to make that value in a view file. I got confused whether I should use some js files or some tpl view file.
This is my code:
<?php
class MymoduleController extends SugarController {
//Can now put actions here
public function action_convert(){
echo "Hello world!";
//return true;
exit;
}
}
How can I map the controller to a view file.

In your controller action method add the following:
$this->view = 'EditView'
Change the 'EditView' to the view you wish to use. The built in MVC stuff is kept in include/MVC and the include/ListView, include/EditView, and include/MVC/DetailView.
If you take a look at say module/Accounts/views. You can see how views are implemented. It is best to create your code in the custom/modules/[module] folder. As this ensures that your changes will not be overwritten when you upgrade SuiteCRM.
You should separate your html with your view using tpls.
if you add the following in the display method of your view:
function display(){
$template = new Sugar_Smarty();
$template->assign('APP', $app_strings);
$template->assign('MOD', $mod_string);
echo $template->fetch('include/ListView/ListViewGeneric.tpl');
}
you can load your custom views.

Related

"Action" in indexController not rendering in view

I have an existing Zend 1.12 project with modules et al that I am trying to simply add a view to create another web page. So there is a controller called "Management" that has an index action, the default of course, and the correct view page called index.phtml. That is all fine and works inside the entire project/website.
class Management_IndexController extends Default_Controller_Action {
function indexAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->setLayout('management');
$this->view->headTitle()->set('Management');
$message = new Com_Ui_Message();
$request = $this->_request;
$settings=new Com_Data();
$content=new Com_Data();
$data = new Com_Data($request->getPost());
$this->setFocus('#name');
}
function chatAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->view->testMessage = "test";
}
}
So if I browse to http://www.bmcmusicgroup.com/management I see the default action public function, and that is all good. If I try to add another public function called chatAction inside of the management controller, and create a view page called chat.phtml in the appropriate folder where the index.phtml file is (the management index.phtml file), and then point my browser to http://www.bmcmusicgroup.com/management/chat I get nothing.
This is an existing site that I am trying decipher. Any pointers would be greatly appreciated.
Try changing your url to the following and you should see the page: http://www.bmcmusicgroup.com/management/index/chat. The URL you entered (http://www.bmcmusicgroup.com/management/chat) decomposes like so:
Module: management;
Controller: chat;
Action: index

how to call a view file from controller(cakePHP)

I am learning cakephp from few days ... so please help a bit thanks .
I made a controller --
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
}
in view had post folder with index.ctp file . I want to ask from where it gets "CakePHP: the rapid development php framework" and from where it takes content . I send this data from controller so it prints only var_dump($posts); ... Thanks in advance.
cake php has default layout files in folder "app/view/Layout" .. file name default.ctp in this folder will be taken as default ..
if you open default.ctp in layout you will see something like
<h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
and other links defined ..you can comment the and check what changes you get in view.
here is from where view gets ""CakePHP: the rapid development php framework"
this line
<?php echo $this->fetch('content'); ?>
in layout fetch the data from controller to show in view which we set using $this->set() in controller
if you want to change the layout create your own in layout folder
and use in contoller like
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
$this->layout = false; // ot you can set ypur own file like 'xyz' for 'xyz.ctp'
}
to set layout for each action in controller use
function beforeFilter() {
parent::beforeFilter();
$this->layout = 'layout';
}
To learn more about layout see http://book.cakephp.org/2.0/en/views.html#layouts
Hope you got me
"CakePHP: the rapid development php framework"
For above look inside views -> layout -> default.ctp file.
Also change default cakephp routing so you can see posts index action when you have posts in URL.
The best way to call a layout from controller is
$this->layout = 'Your layout page name';

Zend Framework 2 set custom layout and setTerminate issue

I dont know if this is a bug of ZF2 or I just dont understand it well, but Im very excited why is this happening.
I'm using a solution to change layout of each module globaly by attaching a Dispatch event. (for example from http://framework.zend.com/manual/2.1/en/modules/zend.view.quick-start.html#dealing-with-layouts, last example)
It's working well, but problem is, when in some action I want to setTerminate(true); (for Ajax call) it will not display only content of controller/action template, but only layout template without content! And that is what I'm not expecting.
This is how to simulate this, set layout in dispatch function (instead of attaching event, to make it cleaner) and then setTerminate in controller's action.
public function dispatch(Request $request, Response $response = null)
{
parent::dispatch($request, $response);
$this->layout('layout/new');
}
public function indexAction()
{
$model = new ViewModel();
$model->setTerminal(true);
return $model;
}
Again, I'm expecting that this will display only content of controler/index template, but instead of that, it display only content of layout/new without content.
I tried to set layout in action, and it working how I'm expecting.
public function indexAction()
{
$this->layout('layout/new');
$model = new ViewModel();
$model->setTerminal(true);
return $model;
}
This is working, it display only content of controller/index template and not layout.
So if I'm changing layout globaly (by attaching dispatch event) for each controller it working until I want to use one of these controllers for Ajax call and use setTerminate.
Thanks for help with that.
When you mark your view model as terminal, listener on dispatch event replaces layout view model with view model you returned.
So it is too late to do $this->layout('layout/new'); after dispatch, you are changing template of your view model.
What you should do is attach listener. For example, from controller itself:
protected function attachDefaultListeners()
{
//do not forget to call parent
parent::attachDefaultListeners();
$events = $this->getEventManager();
//attach before action
$events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'changeLayout'), 10);
}
public function changeLayout(MvcEvent $event)
{
$this->layout('layout/new');
}
That will set layout for your controller, but you will be able to change it from action and setTerminal() will work as expected
If you want to only show the content from the actions view file you can use this approach without an issue:
1) Make a new layout, for ajax calls. This will replace layout.phtml
application/layout/ajax-layout.phtml
<?php echo $this->content ?>
2) Modify your action to ovverride the default layout during Ajax calls
Inside your controller/action :
// Don't render base layout if Ajax call
if($this->getRequest()->isXmlHttpRequest()) {
$this->layout('application/layout/ajax-layout');
}
$model = new ViewModel();
return $model;
this will just render your actions content, and override your base layout :)

How Silverstripe URLs work?

I am new to Silverstripe Framework / CMS. I see ./mysite/code/Page.php as controller and ./themes/simple/... as template directory. I logged into the admin panel and added new test page. The menu appears on the website with the URL http://example.com/test and content is displayed.
So what I want to know is, how to access the new controller let say Download.ss. I want to access the URL http://example.com/download/123/ without adding the new page download in admin panel. Thank you.
First of all, any files with a .ss extension are template files not controllers.
Create a new class in mysite/code/Download.php which extends Controller.
class Download extends Controller {
public function index() {
// Automatically handles URLs like http://example.com/Download
}
public function exampleaction() {
// Automatically handles URLs like http://example.com/Download/exampleaction
}
}
After that you'll want to add a new routes.yml file to the mysite/_config directory to specify that the index function on your new controller should handle calls to http://example.com/download/123.
---
Name: downloadrules
---
Director:
rules:
'download/$ID': Download
Now the '123' portion of your example URL will be accessible as $this->request->param('ID') within the index function.
Now you can do:
class Download extends Controller {
public function index() {
$fileID = $this->request->param('ID');
// Do your thing.
}
}
Documentation for this stuff is at http://doc.silverstripe.org/framework/en/reference/director

Zend Framework 2 display a view within a view

I have two modules Admin and Login.
I want to display the Login view 'login.phtml' within the admin view 'index.html'
I have the following in the Admin modules indexAction controller
public function indexAction()
{
$login = new LoginController();
$view = new ViewModel(array(
'theloginform' => $login->loginAction(),
));
return $view;
}
In the LoginAction method in the Login controller I return the ViewModel for the 'login.phtml' file.
public function LoginAction() {
$view = new ViewModel();
return $view;
}
The indexAction throws an error as the variable 'theloginform' is an object.
Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in...
If i add the following:
$authentication->loginAction()->captureTo('test')
The 'index.phtml' shows a string "content".
I have read that i may need to render the ViewModel before i assign it to the view variable 'theloginform', but i can't seem to get it to work, i have tried the following with no luck.
public function LoginAction() {
$view = new ViewModel();
$renderer = new PhpRenderer();
$resolver = new Resolver\AggregateResolver();
$map = new Resolver\TemplateMapResolver(array(
'login' => __DIR__ . '/../view/login.phtml'
));
$resolver->attach($map);
$view->setTemplate("login");
return $renderer->render($view);
}
If get the following error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "login"; resolver could not resolve to a file
I have even tried adding the DI into the autoload_classmap.php file but still get the same error, i have double checked the login.phtml file is at the correct path:
'/Login/view/login/login/login.phtml' I even copied it to '/Login/src/Login/view/login.phtml'
Very confused have read then re-read the Zend documentation, i just want to pass a view to another view...
If you need share some view content you can use partials for that:
$this->partial('partial/login.pthml', array()); //add this to your index view
you can read about them here
You may also find some usefull information: How does Zend Framework 2 render partials inside a module?
As per this zf2 documentaion page
Write this in login Action:
public function loginAction()
{
return new ViewModel();
}
And in indexAction :
$view = new ViewModel(
array(
//here any thig you want to assign to index view
)
);
$loginView = new ViewModel(
array(
//here any thig you want to assign to login view
)
);
$loginView->setTemplate('moduleName/controllerName/login');
$view->addChild($loginView, 'login');
return $view
In index.phtml you can just echo login <? echo $this->login ?> where ever you want to display loginView.
In ZF 1.x I would likely recommend you build an action helper that is referenced to a view placeholder or a controller plugin that calls back to loginAction for the form logic.
In Zf2 it looks like action helpers have been replaced by controller plugins and seem to be triggered through the event manager and may need to be aware of one or more of the "managers". However the placeholder view helper still exists and even seems somewhat familiar.
I would suggest you look into building/adapting a controller plugin for your login form display that can then be attached to a placeholder view helper. You might be able to get the required functionality with just a view helper, if you're lucky.
I wish I could help more, but I'm still wading through this mess myself.
Good luck.
In you admin view you have to use the render view helper and echo the script rendered, so you can do echo $this->render($this->theloginform);

Categories