Get parameters in actions using zend framework - php

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
}

Related

Use hyphen(-) instead of slash(/) or underscore( _ ) in Routes

I'm Using Codeigniter 3.x , Using routes.php I want to create dynamic routes, for example I have a class name Class1.
I want output url
mysite.com/Class1-Student-Search
But using hyphen(-) is not working
If I put a slash(/), it works,
$route['(:any)/Student-Search'] = "search";
it returns
mysite.com/Class1/Student-Search
and using underscore (_) also work.
$route['(:any)_Student-Search'] = "search";
returns
mysite.com/Class1_Student-Search
But I want to use hyphen(-), if I put it, it will go to 404 error page, I used these four solutions but not work for me.
$route['(:any)-Student-Search'] = "search";
$route['([a-zA-Z]+)-Student-Search'] = "search";
$route['([a-zA-Z-0-9]+)-Student-Search'] = "search";
$route['(.*)-Student-Search'] = "search";
And if i hardcode the value in route
$route['Class1-Student-Search'] = "search";
Then it also working
You trying to create a dynamic routes which is not possible in codeigniter if you see the following flow chart of codeigniter you understand what i mean.
also you can see this chart in codeigniter official website
when you try to redirect or call some url it's work like this
Every request first goes to route there for you can't make it dynamic
Here is my solution, it is working for me, do like this.
$route['(:any)-Student-Search'] = "search";
then in your link button, hopefully in your view, href the link like this.
href="/<?php echo $row->classname; ?>-Student-Search"
the point is that not only you have to make your routes, also suffix it in your href link also the same way.

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.

How to set proper route config in codeigniter framework

we're using codeigniter framework to build application, but we're facing problem to configure '$route' to send correct request.
we just need to setup these route properly.
How i access category:
Category: http://localhost/codeigniter/category/'category-name'/
$route['category/(:any)'] = 'category/index/$1';
Category Post List By Alphabet:
Category Page by List: http://localhost/codeigniter/category/'category-name'/list/'A'/
$route['category/(:any)/lists/(:any)/'] = 'category/lists/$1';
How i access pages:
Page: http://localhost/codeigniter/page/'category-name'/'page-name'/
$route['page/(:any)'] = 'page/index/$1/$2';
we're using rotue something like that, maybe we have problem in it, please check these and let me know how to fix that.
In our codeingiter installation we are using 'codeigniter' dir, 'category' AND 'page' are controllers. in single quotes we are sending values.
Try like
$route['page/(:any)/(:any)'] = 'page/index/$1/$2';
And
$route['category/(:any)/lists/(:any)/'] = 'category/lists/$1/$2';// But Iam not sure
$route['category/(:any)'] = 'category/index/$1';

get Current request referer URL and parse it

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) {
...
}

How can I easily change the text on a page using zend framework and php

What I am looking for is something where I can easily change the text on a page. I want to set it to a default value and if something is present for a specific page to change it
So it would say something like:
I like stackoverflow
But if the value of website was "reddit" it would instead say
I like reddit
So stackoverlflow would be the default, reddit would be something that is set to overwrite it.
edit:added comment
Scenario I: Domain Name based data handling
If you want to work on the basis of domain, then in Zend Framework you can work with customize routers.
Scenario II: GET/POST based input handling
Otherwise if you want to display on the basis of GET or POST input you can place something default value as
$myDynamicVar = $this->getRequest()->getParam('some_key', 'default-value');
Scenario III: subdomain based input handling
Again you will need to have your custom router for the purpose, in addition to having support of wildcard subdomain names with your hosting provider
In a definitions file:
<?php
/* site.php
* customize this file
*/
class Site
{
static public $name = "stackoverflow";
}
?>
In your page file:
<?php
/* page.php */
include_once 'site.php';
echo "I like {Site::$name}";
?>

Categories