user2354302
user2354302

Reputation: 1903

TYPO3 Get URL parameters in PHP controller

How do I get the url parameters in my PHP code in TYPO3?

I have link like this

<f:link.page pageUid="138" additionalParams="{myname: Lilly}">Click!</f:link.page>

The page has a controller attached to it, now how do I get the value of the passed parameter to my controller?

I will have to render Views according to the parameter passed.

EDIT

I'm not sure how to go about this problem.

I have a controller which has actions list and show. If the parameters send to show action is all, then all the items should be displayed and similarly the view changes basing on the parameters passed.

My Question is :

  1. How do I pass the parameters and how to catch them in the Controller?
  2. The link to the action controller is showing really long, and looks very ugly. How do I customize the links? Is there something like routes through which I can achieve this?

Upvotes: 1

Views: 9921

Answers (3)

Shufla
Shufla

Reputation: 872

You just have to use the f:link.action Viewhelper. If you are in list action you could do something like this:

<f:link.action action="show" arguments="{myArgument: myArgument}">Link</f:link.action>

This should switch the view to the view of the show action.

If you would need to catch other arguments, you can $this->request->getArguments() to get all arguments, or $this->request->getArgument('myArgument') to get a specific argument in your controller. Note that this will only contain arguments, cleanly sent with a f:link.action Viewhelper, since those arguments are prefixed with the current extension / plugin you are using.

If you use RealURL, you could provide a configuration to rewrite your URL's to shorter ones.

Edit:

If your link needs to point to another page, you just have to add this:

<f:link.action action="show" arguments="{myArgument: myArgument}" pageUid="myId">Link</f:link.action>

Upvotes: 2

Urs
Urs

Reputation: 5132

If you need more advanced view helpers, there's also the VHS extension http://fedext.net/viewhelpers/vhs.html

Upvotes: 1

biesior
biesior

Reputation: 55798

For fully automatic support of actions you would need to use f:link.action viewhelper however as you discovered yet - it creates too long sets of params (including controller etc.)

The best option in such case is mentioned f:link.page VH however you need to put more effort to find proper actions.

You have several options:

  • If you want/can display these views on separate pages, just create two FE plugins, first will use show action as default one, second - list action. And then you can use simplified params.
  • Use TypoScript for placing plugin on the page, in such case you can use bootstrap your plugin with different default action - depending on TS conditions (if param x exist bootstrap with show, else with list)
  • Use one action for both views, and render a standalone view as a result:

    public function show2Action() {
        $view = t3lib_div::makeInstance('Tx_Fluid_View_StandaloneView');
        if (t3lib_div::_GP('myname') == 'Lilly') {
            $view->setTemplatePathAndFilename(t3lib_extMgm::extPath('yourext') . '/Resources/Private/Templates/Some/Show.html');
            $view->assign('message', 'Hello ' . t3lib_div::_GP('myname'));
        } else {
            $view->setTemplatePathAndFilename(t3lib_extMgm::extPath('yourext') . '/Resources/Private/Templates/Some/List.html');
            $view->assign('message', 'Enter your name');
        }
        return $view->render();
    }
    

Don't forget to validate your data fetched with _GP.

Upvotes: 1

Related Questions