Susitha
Susitha

Reputation: 3349

Symfony store url parameters in sessions

Accessing messages page in a symfony project as follows.

  function messageNavigate(hotelid) {
             window.open('message/'+senderid+'','_blank')
  }

Here it is checking whether user logged in or not. If not, user redirect to login page. My problem is how to redirect him after login for the message page with the previously selected itemid.

routing.yml

message:
    path:     /message/{itemid}
    defaults: { _controller: TestBundle:Message:message }   

login:
    path:     /login
    defaults: { _controller: TestBundle:Security:login}  

seciurity.yml

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false  
    main:
        anonymous: ~
        pattern:    ^/
        form_login:
            login_path: /login
            check_path: /login
            default_target_path: /message/{itemid}
        provider: our_db_provider
        logout:
            path:   logout
            target: /login

Upvotes: 0

Views: 468

Answers (1)

Karolis
Karolis

Reputation: 2618

Instead of checking user login info and redirecting to login page manually, do something like this in message controller:

$this->denyAccessUnlessGranted('ROLE_USER');

This will redirect to login page if user is not logged in. It will also redirect back to original route after user submits login form.

Alternatively, you could probably set this attribute in session:

_security.main.target_path

To the URI where you want user to be redirected after login.

Upvotes: 2

Related Questions