URL Rewriting from .htaccess with Symfony Routing

I have some problems with RewritingUrl with Symfony (2.8) My goal is to redirect the url without having them change in the browser bar.

I need this so that the URL can have a user-readable form and be accessible.

The URL is also used on the form, so the data is submitted on the form ?param=data

In any case, Symfony's internal routing doesn't seem to work quite well (or maybe I missed some things, but I searched up and down all day for this without finding the right answer)

Without frame, in .htaccess If the user went to

www.website.com/search/London/15

I would redirect them to

www.website.com/searchEngine?search_action[city]=London&search_action[radius]=15

Without changing the browser url, so SearchEngine nevers appears with a simple RewriteRule

 RewriteRule ^search/(.*)/(.*)$ /searchEngine?search_action[city]=$1&search_action[radius]=$2 [L]

Symfony ( .htaccess /web forlder, ?),

www.website.com/search/London/15 Symfony 404

, Symfony / , , URL- SearchEngin .htaccess

SearchEngine www.website.com/SearchEngine?search_action[city]=London&search_action[radius]=15

.htaccess Symfony , R RewriteRule

 RewriteRule ^search/(.*)/(.*)$ /searchEngine?search_action[city]=$1&search_action[radius]=$2 [R,L]

, URL- , SearchEngine.

, Symfony , /search/London/15 SearchEngine?search_action[city]=London&search_action[radius]=15 Url?

- , ?

Routing

, URL , .

routing.yml

research_action:
path:     /SearchEngine
defaults: { _controller: "MySearchBundle:Search:search" }

SearchController: searchAction

public function searchAction(Request $request)
{
    /*
       expect search_action[city]=London and search_action[radius]=15
    */
    $form = $this->createForm('MySearchBundle\Form\Type\SearchType',null);
    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid())
    {
         //Get here without problem when from SearchEngine?search_action[city]=London&search_action[radius]=15
           /* 
               This is where Zhuli do the thing
             */

    }
  return $this->render('SomeDefaultPage.html.twig');
}

.htaccess

...
//Basic Symfony Routing stuff
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
#Doesn't work without that R tag
RewriteRule search/(.*)/(.*)$ /SearchEngine?search_action[city]=$1&search_action[radius]=$2 [R=301,L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
.....
+4
1

, - , .htaccess, .

, /search/London/15 SearchEngine

forward_action:
path:     /search/{city}/{radius}
defaults: { _controller: "MySearchBundle:Search:searchForward" }

URL ( ), Request: ParameterBag; SearchEngineAction

  public function searchForwardAction(Request $request,$city,$radius)
  {
        //Putting the attribute in an array
        $query['city']=$city;
        $query['radius']=$radius;
        /*
           Which I pass to the request as a parameter
           The forward will treat this like a Get Query
           ?search_action[city]=$city&search_action[radius]=$radius
         */
        $request->query->set('search_action',$query);
        //Forwarding !
        return $this->forward('MySearchBundle:Search:search',  array('request' => $request));
}

forward(), URL- /Search/Kyoto/25 /SearchEngine?search_action[city]=Kyoto&search_action[radius]=25

, URL- , single .htaccess( )

,

, , ... .

- ?

+1

Source: https://habr.com/ru/post/1686855/


All Articles