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.
Related
This seems like it should be a simple task. I need the current URL from a function within the Controller. This function can be called from multiple actions, and the end goal is to set a form's action attribute. (Side note: It appears IE does not send an ajax request if the URL starts with '#').
I feel like my google-fu is off today because I could not find a good way to do this Zend Framework 2. I have this line currently, but it feels very bulky:
$this->url()->fromRoute(
$this->getServiceLocator()
->get('Application')
->getMvcEvent()
->getRouteMatch()
->getMatchedRouteName()
);
Couldn't you just get the URI from the request object:
$this->getRequest()->getUriString()
Provided your controller extends Zend\Mvc\Controller\AbstractActionController.
Note: This would output the entire URL, like so:
http://example.com/en/path/subpath/finalpath?test=example
If your request route is like this:
http://example.com/en/path/subpath/finalpath?test=example
And You want only this:
/en/path/subpath/finalpath?test=example
You can simply do : $this->getRequest()->getRequestUri()
To specify my Request object is an instance of
\ZF\ContentNegotiation\Request
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'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 know this is a really simple thing that I really should know but I'm trying to learn cakephp without having done much php before. I've been told thats a stupid idea, but I'm doing it for fun and so I'm doing it.
I want to pass an array from one controller action to another controllers action and then pass it to the view. I have:
sponges_controller.php
$info = $this->data;
$this->redirect(array('controller'=>'baths', 'action'=>'dashboard', $info));
baths_controller.php
function dashboard($info) {
$this->set('info', $info);
}
and then
<?php echo debug($info); ?>
in the view for dashboard.
I've tried various ways but can't make it work. All it does is print out Array()
Plz help me! :) Julia
You can't pass data that way from one controller to the other as far as I know, at most you can concat a string to the action, like an ID for view or editing.
If you want to pass the info you could try setting it in the SESSION variable in the following way:
$this->Session->write('Info', $info);
And in your other controller you can check for it:
$this->Session->read('Info');
It looks like cake will not let you pass an array into a controller action. I set up a simple example and I got an 'array to string conversion error'. Is there a specific reason why you aren't just posting the data to baths/dashboard? I can think of a workaround for your problem, but it is quite messy.
8vius's solution above will definitely work.
Here is another way, but using sessions is probably a lot better
$str = http_build_query($info);
$this->redirect('/baths/dashboard?'.$str);
So then in your baths/dashboard action, you will have access to your data using the php $_GET array.
So if you originally had this->data['name'] you can access it with $_GET['name']
I'm not sure about the passing data in different controllers but within the same controller we can do it just like a function call by writing something like this.
$this->function_name($info);
This will perfectly work as intended. I've not tried this type of data passing in different controllers function.
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>