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).
Related
I'm using this plugin to create slug-based URL's on a CakePHP 2 web application: https://github.com/josegonzalez/cakephp-dynamic-route
The documentation suggests that you would call a Cake controller like so:
posts/view?id=45
My URL's currently work as Cake's default behaviour. So using the example above posts/view/45 works but posts/view?id=45 does not.
When I call URL's as per the example I get a 404 error.
My functions are written like so (e.g. in PostsController.php):
public function view($id) {
// logic to load post by ID
// ...
}
There is almost no documentaiton for the above plugin. Has anyone used it or know where I'm going wrong? It seems you cannot pass a GET variable such as 'id' to the 'view' function, without re-factoring the code inside it to accept passed parameters?
The solution appears to be that some of the controller functions needed to be re-written to accept GET style parameters.
In the documentation a "spec" field looks like this:
posts/view?id=45
In a regular CakePHP application the route for that would be like this: posts/view/45
The plugin simply doesn't work if you put the second style of route (posts/view/45) into the "spec" field.
So the answer is the "spec" fields must be like so:
posts/view?id=45 and then your controller functions have to be re-written, e.g.
public function view($id) {
if (isset($this->request->params['id'])) {
$id = $this->request->params['id'];
}
}
Doing this means that it will work with a parameter (view?id=45) or a standard Cake call (view/45).
Please note this has nothing to do with the "slug" aspect of the plugin - the "slug" can be anything, as per the documentation examples: /why-isnt-this-pup-asleep or /manchester/cakephp-developers-dance-to-beyonce. The original question was asking if there was a way to map a "spec" given in the documentation to a Cake controller function without having to modify it like I have above. The answer seems to be no, you have to modify them!
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');
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.
I am using the Zend framework and what I need is to construct a url in my view. Normally in regular php code I'd just grab the GET Variable by using the global $_GET. However with Zend I'm setting it to clean URIs so :
?ac=list&filter=works&page=2
Looks like
index/ac/list/filter/works/page/2
In my view I'm setting a links cs such that if the GET variable filter equals works then the color of that link would be different and it would point to the same page only linked as so:
index/ac/list/filter/extra/page/2
And like wise I have a number of other links all which just one GET value - how do I set this up - I am using the Zend framework.
To access a request variable direct in the view you could do:
Zend_Controller_Front::getInstance()->getRequest()->getParam('key');
But as others have said, this is not a good idea. It may be easier, but consider other options:
set the view variable in the controller
write a view helper that pulls the variable from the request object
If you need to access a GET parameter from a view, i think you're doing it the wrong way.
I suggest that you set up a route with all your parameters, and then use $this->url from your view to render a valid and correct url.
Fore som more info, check out the following blog post (no, i'm not the author):
http://naneau.nl/2007/07/08/use-the-url-view-helper-please/
Edit:
If you want to be 'lazy', you can set a view parameter from your controller by doing $this->view->param = $this->_getParam('param'). You can then access param from your view by doing echo $this->param;. However, i do not recommend this.
To access the Request Object one way that is common is to save it in the Registry.
http://osdir.com/ml/php.zend.framework.mvc/2007-08/msg00158.html
http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-access-request-object-customizing-layout-view-3349.html
You can pass it in from a controller: $this->view->page = $this->_getParam('page');.
Footnote: I agree with #alexn.
i am using Zend Framework v1.11 and i am doing like this
In Controller
$this->view->request = $this->_request;
then in View you can access any Request param like this
<h3><?= $this->request->fullname ?></h3>
Rather than using controller/action/key1/value1/key2/value2 as my URL, I'd like to use controller/action/value1/value2. I think I could do this by defining a custom route in my Bootstrap class, but I want my entire application to behave this way, so adding a custom route for each action is out of the question.
Is this possible? If so, how would I then access valueN? I'd like to be able to define the parameters in my action method's signature. e.x.:
// PostsController.php
public function view($postID) {
echo 'post ID: ' . $postID;
}
I'm using Zend Framework 1.9.3
Thanks!
While I don't think it's possible with the current router to allow N values (a fixed number would work) you could write a custom router that would do it for you.
I would question this approach, however, and suggest that actually listing all of your routes won't take long and will be easier in the long run. A route designed as you've suggested would mean that either your named parameters are always in the same order, i.e.
/controller/action/id/title/colour
or that they are almost anonymous
/controller/action/value1/value2/value3
With code like
$this->getRequest()->getParam('value2'); //fairly meaningless
Does it have to be N or can you say some finite value? For instance can you imagine that you'll never need more than say 5 params? If so you can set up a route:
/:controller/:action/:param0/:param1/:param2/:param3/:param4
Which will work even if you don't specify all 5 params for every action. If you ever need 6 somewhere else you can just add another /:paramN onto the route.
Another solution I've worked with before is to write a plugin which parses the REQUEST_URI and puts all the extra params in the request object in the dispatchLoopStartup() method. I like the first method better as it makes it more obvious where the params are coming from.