Ad

Is There A Way To Redirect With GET Parameters In October CMS?

- 1 answer

I have a component which has an Ajax Handler. At the end of this handler I would like to redirect the user to a page with GET Parameters.

Example:

  • User goes to page www.example.com/hello
  • He clicks on button with Ajax Handler onClicked()
  • Users browser gets redirected to www.example.com/redirect?id=1

In the Ajax Handler some code will return an ID which is from an REST API. The same Handler knows also about the page to redirect to (which can be set in the Backend Settings). I tried to find something in the October CMS Documentation. The only thing I found was the Redirect Class which can redirect to a given page but I couldn't find a way to set the GET Query parameters

<form id="sample-form" data-request-validate data-request="onOrder">
    <input type="hidden" name="sample" value="{{ sample.id }}" />
    <button type="submit">Get Sample</button>
</form>

AJAX Handler

public function onOrder() {
    $sample = Sample::findOrFail(input('sample'));
    // API Call returns ID $id
    return Redirect::to(Settings::get('redirectUrl')->with('id',$id);
}

I expect the user to be redirected to the given Url with GET Query

Anybody an idea how to make this work?

Ad

Answer

I use the Input; service at the top of the component page. Now in my opinion it is best to use the least amount of AJAX as possible. For doing a full page redirect I recommend a simple HTML Form but opening it using TWIG.

So my trouble shootings tips. Create your form with {{ form_open({request: __SELF__~"::onOrder", id: "sample-form"}) }}; don't forget the ending {{ form_close() }}. This will generate a non ajax form but useful for trouble shooting. Notice the __SELF__~"::onOrder", this is for specifying a specific component's handler. This is important if you have numerous components that has the same onOrder handler and good practice in general.

Now you will notice in the Component.php onOrder handler I have //dd(Input::get('sample')) when you un-comment this line and run the code you should see the $id that you are wanting to pass. If it is correct then move the die and dump command farther down the function.

Check to see if $sample is retrieved. You could just use Sample::where('id', $id); I believe this will return null if not found but if found you can then use $sample->first() because where returns a collection.

Building the redirect is easy return Redirect::to(Settings::get('redirectUrl').'?id='.$id);. Here I am concatenating the '?id='.$id at the end of the url. Including a ->with() just means you are flashing a message like `->with('success', 'Thank you for your order'); You can retrieve this message through the session.

I did include the {{ form_ajax(__SELF__~"::onOrder", {id: "sample-form"}) }} if you feel you must use Ajax and to answer your question completely. I did test this, albeit not the exact scenario, and all is working for me.

    <?php namespace Author\Plugin\Components;

    use Input;
    use Author\Plugin\Models\Sample;


    class Something extends \Cms\Classes\ComponentBase
    {
        public function componentDetails()
        {
            return [
                'name' => 'Somthing',
                'description' => 'Something'
            ];
        }
        public function onOrder() {
            //dd(Input::get('sample'));
            $id = Input::get('sample');
            $sample = Sample::where('id', $id);
            //dd($sample);
            // API Call returns ID $id
            if ($sample != null) {
            return Redirect::to(Settings::get('redirectUrl').'?id='.$id);
            }
        }
    }
    {{ form_ajax(__SELF__~"::onOrder", {id: "sample-form"}) }}
        <input type="hidden" name="sample" value="{{ sample.id }}" />
        <button type="submit">Get Sample</button>
    {{ form_close() }}

    {{ form_open({request: __SELF__~"::onOrder", id: "sample-form"}) }}
        <input type="hidden" name="sample" value="{{ sample.id }}" />
        <button type="submit">Get Sample</button>
    {{ form_close() }}
Ad
source: stackoverflow.com
Ad