get Current request referer URL and parse it - php

i have a view (grid) which is used as a rendepatial. this renderpartial grid is used in two actions.
<div id="activities"><?php echo $this->renderPartial('grid', array('model'=>$model)); ?></div>
i would need to know how can i check which action is requesting now ? i need to stop some scripts generating from yii and add some scripts when action is 'default/index/' (i mean the call is coming from default/index/) this could also be called from this way ... search/default/index/area/act?query=
action A - http://mylocalurl.com/ijob-css/index.php/search/default/index/area/act?query=
action B - http://mylocalurl.com/ijob-css/index.php/activities/default/index/
for action b only i need to add some scripts and wondering how this can be done in yii ?
is there something like this available in yii ? if(yii->app->getRquest == 'action B')
EDIT 1
I tried it this way,
echo Yii::app()->controller->action->id . " - " . Yii::app()->controller->id . " - " . Yii::app()->controller->module->id;
it returns, //sessionIndex - default - activities from both actions.
but what i wanted to do is, from where did user come to this path ? his last URL.
so i can easily track weather its action 1 or action 2
EDIT 2
tried this way too, echo Yii::app()->user->returnUrl;
but it prints onlty - /ijob-css/index.php
EDIT 3
This does that but need to know is there any better way to get it ? $_SERVER['HTTP_REFERER']; also this retuns the entire url like this http://mylocalurl.com/ijob-css/index.php/search/default/index/area/act?query=
but i only needed "search/default/index/area/act?query=" in fact the model id is even fine alone which is "search"

To get action ID use:
Yii::app()->controller->action->id
Similar for controller:
Yii::app()->controller->id
And for module use:
Yii::app()->controller->module->id
You can also use:
Yii::app()->request->pathInfo
For Yii preprocessed path info

$route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());
echo $route;
But module, controller, action can be deleted, I advice to give addition parameter to grid
$this->renderPartial('grid', compact(
'model',
'flag'
));
//grid
switch($flag) {
...
}

Related

set config params from database in yii 1

I have created CRUD for global configuration parameters. I want to apply this parameters value as main config params (main.php).
I have found some way like add value of these parameters to any .inc file and perform read/write operation. Can anybody help me how can I achieve this? I am beginner in yii.
I have created table structure :
global_config :
Field | Value
pageSize | 20
admin_email| admin#example.com
main.php file as below :
.
.
{
'params' = array(
'pageSize' => 10,
'admin_email' => 'admin#example.com',
);
}
.
.
I am using config file as show above, I want to change it dynamically that it should get value from database.
So that I can make changes in config file from front-end side. I don't need to perform open/write action on main.php
In yii1 you can use params you can set this in main.php
'params'=>array(
'your_param'=>'your_value ',
...
Yii::app()->params['your_param'];
and you can set this value like a simple array poplulating the value form database
$param['yuor_param' =>$your_db_value];
You can't do this for params. As Application parameters are not really meant to be altered and if you change a value, it does not persist across different requests.
These are treated as constant in Yii, So you can't define them after running the script as config files runs at first as your code runs.
Finally, I found solution as per my requirement. I followed given below link from yii forum.
Link : http://www.yiiframework.com/wiki/304/setting-application-parameters-dynamically-in-the-back-end/
Thanks.

giving custom argument vlue in 'q' variable

**also posted on druapl.stackexchange
https://drupal.stackexchange.com/questions/150500/giving-custom-argument-vlue-in-q-variable
**
apologies if question is ambiguous . the scenario is as follows:
in drupal 7 , we want to use a custom template page when a specific value is given for the q variable in url .
for example if we give http://localhost/drupal/?q=xyz/123 , we want to use a custom template page say page-xyz.tpl.php ..
have a hunch that hooks and template.php file may be the key components here but not sure what to exactly do..
any help appreciated.
you could implement theme_preproccess_page() (or node, or html) to control this in your template.php
function YOURTHEME_preproccess_page(&$vars) {
if (isset($_GET['q']) && $_GET['q'] == 'xyz/123') {
$vars['theme_hook_suggestions'][] = 'page_xyz';
}
}
that should work, but I would like to recommend not use the '?q=xyz' solution, but do an preproccess that should work to all your pages, like this.
function YOURTHEME_preproccess_page(&$vars) {
$title = strreplace(' ','_', $vars['node']->title);
//if you use the transliteration module, instead of strreplace
//use transliteration_get($vars['node']->title);
$vars['theme_hook_suggestions'][] = 'page_'.$title;
}
now that should work for every page that you want to make a custom template. Just add the file and clear the chaches. If you don't have the page template to the specific page, it's ok, drupal will use the default.

Get parameters in actions using zend framework

How to get parameters from below url
domain.com/admin/edit/12
I want to access this value (12) in edit function.
I searched around but didn't found any inbuilt solution in zend framework.
Even in other framework it works easily.
Like in codeIgnitor it works as segment and function parameter.
How I can see from code , you have Admin_Controller & some action edit ( by default routing) . For getting edit value , U need generate urls like domain.com/admin/edit/id/12/ (for example) . And than in action edit use next:
$id = $this->_request->getParam('id',0);
if ($id){
//get info for edit by ID
}
EDIT
IF you still want urls like domain.com/admin/edit/12, do next:
$uri = $this->_request->getRequestUri(); // or $this->getRequest()->getRequestUri()
$id = intval(end(explode('/',$uri)));
if($id){
// do something
}

How to remove action name from url in cakephp?

I am working on cakephp project. I have removed index.php from URL using .htaccess file and now I want to remove view name from URL & add some other two varying parameter. Suppose I choose country & city then these two parameter should appear in URL on selecting them.
The problem I am facing is as cakephp takes
www.example.com/Controllername/viewname
But my requirement is like this
www.example.com/Controllername/param1/param2
If I pass this way It looks for param1 as controller and param2 as view.
Initially should be like:
www.example.com/Controllername/
In your APP/routes.php:
// www.example/com/Controllername
Router::connect('/Controllername',
array('controller'=>'Controllername', 'action'=>'index'));
// www.example.com/Controllername/param1/param2
Router::connect('/Controllername/:param1/:param2',
array('controller'=>'Controllername', 'action'=>'index'),
array('pass' => array('param1', 'param2')));
and your controller:
// set to null/a value to prevent missing parameter errors
public function index($param1=null, $param2=null) {
//echo $param1 . ' and ' . $param2;
}
When generating links:
array('controller'=>'Controllername', 'action'=>'index', 'param1'=>'foo', 'param2'=>'bar');
Order matters. Change paramX to anything you want i.e. country and town
note this does not cover: controllername/param1 - both must be present in this example.
There are other ways to achieve this.
I think you should first make sure that mod-rewrite module is enabled. You shouldn't have had to remove index.php from the url using .htaccess if mod_rewrite were enabled. Check how to enable it in the manual of your webserver and the default .htaccess of cakephp should be able to handle the rest of the routing for you.
After you have enable rewrite module, you can modify the routes as pointed out by #Ross in the previous answer in you APP/routes.php:
// www.example/com/Controllername
Router::connect('/Controllername',
array('controller'=>'Controllername', 'action'=>'index'));
// www.example.com/Controllername/param1/param2
Router::connect('/Controllername/:param1/:param2',
array('controller'=>'Controllername', 'action'=>'index'),
array('pass' => array('param1', 'param2')));

CakePHP session ID path or other method to share the results of a url - recommendations welcome

I am looking for suggestions on a sensible Cake approach to creating a session id based url that I can share with others to see the same search results I am seeing on my end.
I know in standard php, I would just get the session id and pass it to the url. But my guess Cake probably has a method or approach for this exact thing (my guess). I have not been able to locate anything specific yet.
Any ideas as to the best approach to this with Cake methods? Or will I need to re-invent the wheel on this?
Are you asking this because you are using POST and consequentially the URL does not include the search parameters, as it would through GET?
I use the following design paradigm for search on all the apps I build.
Search form submits as a POST.
In the controller action, if the form is submitted as a post, I extract the search parameters, and then redirect to a URL that includes the (named*) parameters.
So the action code might look something like this:
function search() {
if($this->RequestHandler->isPost()) {
// let's say we extract parameters called $a and $b here
$this->redirect(array(
'action' => 'search',
'a' => $a,
'b' => $b
)
exit();
)
// perform the normal search operation
// the 'a' and 'b' parameters can be accessed in the $this->params['named']
// array, automatically extracted by the CakePHP router using the configuration below.
}
With regard to named parameters, I would have this set up in my routes.php:
Router::connectNamed(array('a', 'b'));
This results in the redirect above creating a nice, clean-looking URL like:
http://example.com/controller/search/a:FOO/b:BAR
I solved this:
search ( ... ) {
if ($this->RequestHandler->isPost()) {
$this->Session->write('search_form_sess', $this->data);
$initial_url = $ApplicantAge . '/' . $SpouseAge . '/' . $NumberChildren . '/' . $Vision . '/' . $Zip;
$this->redirect(array('action' => 'search', $initial_url));
exit();
}
...
}

Categories