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>
Related
Using Symfony to access url parameters in an action like this
$this->getRequestParameter('myUrlParameter');
How can I access this parameter in the root template, where I am not within the action? I tried many things already like
echo $context->getRequest()->getParameter('pool');
and
echo $context->getParameter('pool');
Which didn't work. With
echo sfContext::getInstance()->getRouting()->getCurrentInternalUri();
I get the whole url - which is not what I want.
I feel like there must be a simple way to access this information ...
You can access the request object from the template directly like this:
$pool = $sf_request->getParameter('pool');
You need to pass it to the view via controller:
public function yourController(sfWebRequest $request)
{
$this->myUrlParameter = $this->getRequestParameter('myUrlParameter');
}
Trying to access it directly, you are fighting with the framework, not using it.
I have a route like this:
Route::get('demo/{system?}', 'MonitorController#demo');
I am using it like so because I would like my url to look like so:
mysite.com/demo/spain-system
Where spain-system will be the variable I need to get.
Right now, I'm getting it like this:
public function demo($systemName = null){
}
But I would like to be able to access to it as if it were a URL parameter with Input::get('system') so I can access to it from other methods or even from other controllers such as BaseController.php.
Is there any way to achieve this?
I've played around with Route::input('system') but then it doesn't work when I pass it as a get parameter (in other Ajax calls and so on)
Update
In PHP we can get URL params by using the $_GET function and laravel provides the function Input::get() to do so as well.
If there were no routes in laravel, I would make use of .htaccess rewrite rules to change this:
mysite.com/demo/?system=spain-system
To this:
mysite.com/demo/spain-system
And I could still retrieve the variable system as a GET parameter by using $_GET["system"].
That's kind of what I would expect of laravel, but it seems it is just treating it as the parameter of the demo method and not really as a URL variable.
Is there any way to keep treating it as a URL variable and at the same time use it in a pretty URL without the ?system= ?
So you actually just want to get an url like this? mysite.com/demo/spain-system instead of mysite.com/demo/?system=spain-system? Laravel provides that by default?
Look, When you want to get the router variable {system?} to be accesible you'll need to do this:
In your router:
Route::get('demo/{system}', 'MonitorController#demo');
Then you have an controller where this noods to stand in:
public function demo($system)
{
//your further system
//You are be able to access the $system variable
echo $system; //just to show the idea of it.
}
When you now go to to localhost/demo/a-system-name/, You'll see a blank page with a-system-name.
Hope this helps, because your question is abit unclear.
Right now, if I have a parameter in a URL in one of my Laravel projects, I have to detect the route and grab the parameter:
Route::get('mission/{name}', 'MissionsController#show');
Pass the $name parameter as an argument to my controller:
class MissionsController extends BaseController {
public function show($missionName) {
...
}
}
Then pass it along to the view that is returned in my controller method:
return View::make('missions.mission', array(
'name' => $missionName
));
Before then using the $missionName variable in my view:
<p>{{ $missionName }}</p>
This is quite a roundabout way of doing so. Is there any way I can get the parameter from the URL directly in my view? I've tried accessing the $_GET superglobal, but it is empty. Surely there must be a better way of doing this.
Thoughts?
Use this code :
{{ Route::current()->getParameter('theParameterName') }}
EDIT: Above doesn't seem to be supported anymore in recent Laravel versions. You should use #lukasgeiter answer instead:
Route::input('name');
There is a nice shortcut for Route::current()->getParameter():
Route::input('name');
For small projects or simple examples, it may seem like this is a "roundabout" way, however this is the way it should be. In order to create more reusable, quality code, you need to have this separation of concerns. An over-simplified idea follows.
It is your route's job to figure out which controller needs to be called, and to make sure it is called with the correct parameters.
It is your controller's job to read the state of the application (input), communicate with the model (if needed), send the data into the view, and return the response. There's plenty opinion on whether or not this violates the Single Responsibility Principle, but no need to go into that here.
It is the view's job to use the data passed to it to build the response. The view shouldn't care where the data came from or how it was gotten, only that it now has it and can do what it needs. Your $missionName should be able to come from a URL segment, a URL request variable, a field on a model, or any other place you can think of, but the view shouldn't know any of that.
I am using Zend framework and it does URL rewriting
but I want to handle in controller just regular querysting, get request
seomthing like this transactionsExternal.phppage=1&start=0&limit=100&sort=threadid&dir=ASC&callback=Ext.data.JsonP.callback1
Can somebody tell me how to get this GET request variables in controller? considering that Zend prevents me just to use variables like $_GET[something]
$this->getRequest()->getParams(); also is not returning anything
ZF does not prevent you using $_GET, although doing so is discouraged. $this->getRequest()->getParam('start'), or $this->getRequest()->getParams() will give you the GET params. So if this isn't working for you something else is going wrong somewhere.
Is 'transactionsExternal.php' definitely part of your ZF app?
How are your controllers and actions set up?
Zend Framework 1 works with key/pair values for get parameters. So you might have a transaction controller and a get action. A request would look like so:
domain.com/transactions/get
To append and ID GET parameter you would do:
domain.com/transactions/get/id/10
Then In your controller action you would do the following to get the id value:
$request = $this->getRequest();
$request->getParam('id');
I am using Yii.
if my url is http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
My real url (file path) is only http://localhost/evengee
How would I obtain, preferably in the view:
full url http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
url without explicit parameters http://localhost/evengee/event/page/id/2/sfn+master/
I know I can split/explode str_replace by the ? and use $_SERVER. Would prefer to use
Yii native methods.
For:
full URL (http://localhost/even/page/id/2/?t=3) use
Yii::app()->request->getUrl(),
URL without parameters (http://localhost/even/page/id/2/ use
Yii::app()->request->getPathInfo().
Second one will give you the URL without schema, server and query string. This seems good enough.
To get the full URL, use the getRequestUrl() method of CHttpRequest
Yii::app()->request->getRequestUrl();
You can get the controller, module and action name of the current page from the CApplication methods
Yii::app()->getController()->id; //gets you the controller id
Yii::app()->getController()->getAction()->id; //gets you the action id
You can piece together a URL using the baseURL property of CApplication
Yii::app()->baseURL
The best and shorter way I found is:
Yii::app()->createAbsoluteUrl(Yii::app()->request->getPathInfo());
OR
$this->createAbsoluteUrl(Yii::app()->request->getPathInfo());
For Yii2, use \yii\helpers\Url::canonical().
Documentation: https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#canonical()-detail