In Symfony ( PHP ), what is the path of setTemplate method? - php

I am using the following code in action class of a module XYZ:
$this->setTemplate("abc.php");
In which directory, it is trying to find abc.php?

The corresponding path is the templates directory that is parallel to the actions directory in which you are editing the actions.class.php file.
Within the file /path/to/my/module/actions/actions.class.php, if you do this:
$this->setTemplate("index");
The corresponding template file will be located at:
/path/to/my/module/templates/indexSuccess.php
This of course assumes successful completion of the action; upon failure, the handling is different and depends on your implementation.
Also worth noting:
If you don't set a template manually using $this->setTemplate($foo), the template name is determined by convention. For the action function executeHomepage(sfWebRequest $request),
the template homepageSuccess.php will be shown by convention (again, unless you specify otherwise with setTemplate).

See http://www.symfony-project.org/gentle-introduction/1_4/en/06-Inside-the-Controller-Layer#chapter_06_sub_action_termination. This documentation page contains comprehensive list of possible template names.
To be clear. Call
$this->setTemplate("abc");
will force Symfony to render template module/templates/abcSuccess.php if coressponding action method returns sfView::SUCCESS or nothing.
module/templates/abcError.php will be rendered if your action will return sfView::ERROR.
Also if your action is terminated with return 'SomeString'; then module/templates/abcSomeString.php is to be rendered.

According to API Documentation:
any time i write this:
$this->setTemplate('action_name', 'module_name');

Related

Changing the behaviour of view in Codeigniter

I am using codeigniter for a project that is used by a variety of companies.
The default version of our software is up and running and works fine - however some of our customers want slightly different view files for their instance of the system.
Ideally what I would like to do is set a variable (for example VIEW_SUFFIX) and whenever a view file is loaded it would first check if there was a suffix version available if there was use that instead.
For example if the system had a standard view file called 'my_view.php' but one client had a VIEW_SUFFIX of 'client_1' - whenever I called $this->load->view('my_view') if the VIEW_SUFFIX was set it would first check if my_view_client_1 existed (and if it did use that) or if not use the default my_view.php.
I hope that my question is clear enough... If anyone has done this before or can think of a way to do it I would really appreciate it.
EDIT:
Ideally I would like a solution that works without me changing every place that I am calling the view files. Firstly because there are a few files that may want different client versions and also because the view files are called from a lot of controllers
I had a similar requirement for which I created a helper function. Among other things, this function can check for a suffix before loading the specified view file. This function can check for the suffix and check if the file exists before loading it.
Unfortunately, the file checking logic would be a bit brittle. As an alternative, you can implement a MY_Loader class that will override the basic CI_Loader class.
Something like this in your application/core/MY_Loader.php:
class MY_Loader extends CI_Loader {
protected function _ci_load($_ci_data)
{
// Copy paste code from CI with your modifications to check prefix.
}
}
Could you not do this
// some method of creating $client
// probably created at login
$_SESSION['client'] = 'client_1';
$client = (isset($_SESSION['client'])) ? $_SESSION['client'] : '';
$this->load->view("your_view{$client}", $data);

how to override files in [mysite]/concrete5/core

I want to override the guestbook functionality. To be exact, I want to override the action_form_save_entry() function on [mysite]/concrete5/core/controllers/blocks/guestbook.php
I've tried to override it these ways:
[mysite]/controllers/blocks/guestbook.php
[mysite]/core/controllers/blocks/guestbook.php
noe of them works. I can't find any way how to override that file. The documentation here and here doesn't show how to override that /core/ directory. Their forum never helps. Google result also just get misled with the 'core' keyword. All the result just take the 'core' meaning as just what's exist on the /concrete5/ directory, not the exact true /concrete5/core
Looks like that /concrete5/core/ directory appear only on the newer version. CMIIW.
Btw, maybe I should also tell you what I want to do with that function. Probably you have another workaround for this instead of simply overriding it. I want to add SMS notification functionality to it. So whenever someone submit a new comment, an SMS would be sent to the admin of a particular page.
Yes, the /concrete/core directory structure is new to 5.6. Tutorials and documentation on c5 can be ... lacking ... but in this case it's just a matter of them being behind a bit.
The "real" guestbook controller is at /concrete/blocks/guestbook/controller.php. You'll notice that it's just a shell of a class:
class GuestbookBlockController extends Concrete5_Controller_Block_Guestbook {}
The file that you referenced defines Concrete5_Controller_Block_Guestbook.
So, the solution is to override the real controller, not whatever it extends (ie, the file that you were looking at). Thinking in this way, it should be clearer that you need to create a file at /blocks/guestbook/controller.php. In fact, just copy the controller.php that I referenced above because you need to keep the (sometimes multiple) classes. Then, you can override the particular function. (Don't forget to call parent::action_save_form_entry()).

Modifying Zend_ViewHelper_Pagination_Controller Partial-Path

I've got a Zend-Framework application. I'm using the module-structure which Zend_Controller_Frontprovides. Here is a small excerpt from my directory-structure (only the important parts for this question):
root-directory
- modules
- blog
- views
- scripts
- index_index.phtml
- views
- pagination_control.phtml
As you can see I've got view-scripts that are specific to a module/controller/action. These views are located in the corresponding path (in this case like modules/blog/views. I've also got a more general view-directory located in the root-direcetory of my application.
What I am doing now is to call the PaginationControl-ViewHelper in modules/blog/views/scripts/index_index.phtml. This View-Helper however renders a partial-view, as you know. The ViewHelper tries to locate the partial-view within the same directory (meaning modules/blog/views/scripts. Since I want to use the same view-partial-script (pagination_control.phtml) in different modules I want to make this view-partial accessable from each module. So I want to put that file in the general views-folder in the root-directory.
However this doesn't work. The ViewHelper always looks for the view-script in the corresponding module-folder.
Anyone can help to make it accessable from my general views-directory?
As you can see here, since ZF 1.6.2 pagination control can take an array instead of a string for the partial argument, and in this array you set 1st the name of the partial and in 2nd the module name. This is still undocumented.
Using an array you can specify a module ('common'?) for the partial to use.
The real call will be (with $partial your 3rd argument to the paginationControl() view helper ):
$this->view->partial($partial[0], $partial[1], $pages);
This is usefull if you have a 'common' module.
Now here you are using a shared folder. You shoudl have installed it as a shared folder for your Zend_View this way (in a Boostrap or ressource code):
$view->addScriptPath("/root-directory/views");
or better:
$view->addScriptPath("/root-directory/views/partials");
And then you should'nt be required to specify any module directory. Zend_View should always check for a partial in this folder.

Symfony - get module URL-key

I'm using a common set of javascript across several Symfony modules. I'd like to output the current module url-key as a javascript variable, so the javascript can use it to construct urls for various AJAX calls. I can't find where to get it, though.
$sf_context->getModuleName();
returns the module name, but not how it appears in the URL. I get that I could parse the module name from window.location, but that not only seems a bit crude, but I will soon have a case where I construct a url to a different module than the one that generated the current page.
How does one get the URL-key for the current (or given) module? Surely that mapping exists for the front controller.
To get the exact value as it was written in the url you can use custom routings. In symfony you can create a routing rule with the module name as a variable in the url. This variable named whatever you want will then be available in your action.
For example (app/*yourAppName*/config/routing.yml):
the_name_of_your_route:
url: thisIsAUrl/:variableYouWantWithWhateverCase
param: { module: yourModuleName, action: yourActionName }
Your url: param can even have regEx and wildcards in them if you like.
So now when you are in your executeYourActionName function a variable named variableYouWantWithWhateverCase will be set in the request exactly how it was typed in the url.
Hope this helps.
You can get the url-value of the current module with:
$sf_params->get('module'); // in templates
and
$request->getParameter('module'); // either this
$this->getRequestParameter('module'); // or this in the actions
and for constucting URLs to other modules I can use url_for(), though I couldn't get the module url-value alone via this method (it generates an entire url).

Symfony - Override sf_format when calling get_partial

I'm making an AJAX call in my symfony project, so it has an sf_format of 'js'. In the actionSuccess.js.php view, I call get_partial to update the content on the page. By default it looks for the partial in 'js' format since the sf_format is still set as 'js'. Is it possible to override the sf_format so that it uses the regular 'html' partial that I already have (so that I don't have to have two identical partials)?
I have had a similar issue.
I looked through the code, and get_partial doesn't give you any scope to change the format looked for ... guess you could modify the code to make that possible if you needed to.
I instead went for switching the request format - also not ideal in my opinion. But better than editing the symfony files.
To do this in the controller:
$request->setRequestFormat('html');
or in the view
$sf_context->getRequest()->setRequestFormat('html');
In both cases, if you want to set this back afterwards, you can retrieve the existing value using getRequestFormat().
if your looking for a more sustainable solution, you could listen to the view.configure_format and set the sfPHPView extension in your appflication configuration.
// in apps/api/config/apiConfiguration.class.php
public function configure() {
$this->dispatcher->connect('view.configure_format', array($this, 'configure_formats'));
}
public function configure_formats(sfEvent $event) {
// change extension, so our module templates and partials
// for xml do not need the .xml.php extension
$event->getSubject()->setExtension('.php');
}

Categories