Hide GET parameter from URL

后端 未结 5 1440
故里飘歌
故里飘歌 2020-12-18 08:44

How to hide URL GET parameters (http://domain.com/MyFirstYii/page?view=about). I\'ve searched lot of posts. They all are saying about rewrite and URL manager, but i couldn\'

相关标签:
5条回答
  • 2020-12-18 09:18

    If you intend to use GET and need these parameter, you can't hide it, as this is the way GET works. If you really need to hide the parameter, you must switch to POST, as then parameters will be passed in the request payload instead of in url.

    0 讨论(0)
  • 2020-12-18 09:24

    use post method instead of get....that's the best and efficient solution.

    to follow up on your query check out this site:

    [http://pure-essence.net/2007/06/29/simple-php-path-rewrite/]

    0 讨论(0)
  • 2020-12-18 09:25

    \w in regexp means „word“ character and such url part as „my-prety-page“ will NOT match. To hide GET params you must improve your urlManager rules. You can write such a rule for pages using SEF urls:

    '<controller:\w+>/<id:\d+>/<title:[^\/]*>/*' => '<controller>/view'
    

    In this case when you enter url

    http://example.com/page/12/my-prety-title
    

    a Page controller will be called to perform view action with id and title as arguments. It is the same if you enter this url:

    http://example.com/page/view?id=12&title=my-prety-title
    

    The last part /* in rule allows to keep additional params. E.g. if your address is

    http://example.com/user/55/john-doe-junior/foo/bar/
    

    in UserController's actionView you can write

    echo '<pre>' ;
    print_r($_GET);
    echo '</pre>' ;
    die();
    

    and you'll see

    Array
    (
        [id] => 55
        [title] => john-doe-junior
        [foo] => bar
    )
    
    0 讨论(0)
  • 2020-12-18 09:32

    uncomment these line from main.php

    'urlManager'=>array(
                            'urlFormat'=>'path',
                            'showScriptName'=>false,
                            'rules'=>array(
                                    'MyFirstYii/post/<view>'=>'MyFirstYii/post', 
                                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                            ),
                    ),
    

    And put .htaccess file in root directory of project and write following code

    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    
    0 讨论(0)
  • 2020-12-18 09:36

    Add this url rule to the top of your url rules:

    'page/<view:\w>' => 'user/page'
    

    I'm assuming the next:

    • controller name: UserController
    • action name: actionPage

    If my assumptions are wrong, please provide the right controller name and action name, so I can fix the answer.

    UPDATE: fixed controller name based on comment

    UPDATE2:

    If you want this to work for all actions in your controller, use:

    '<action:\w>/<view:\w>' => 'user/<action>'
    
    0 讨论(0)
提交回复
热议问题