简体   繁体   中英

Custom URL Helper redirect with get method

On my codeigniter application/helper/MY_url_helper I would like to be able to add '?&route=' . $uri '?&route=' . $uri I would like to be able to use my '?&route=' after the index.php but before the first folder / function.

But every time I click on my link the url changes but the page stays the same?

Question: On my helper how can I add '?&route=' . $uri '?&route=' . $uri but make it still redirect to page currently each time even when I click on link the url changes but page stays the same?

redirect('common/dashboard');

Url http://localhost/project/admin/?&route=common/dashboard

I have tried it with routes and same issue.

<?php

if ( ! function_exists('redirect'))
{
function redirect($uri = '', $method = 'auto', $code = NULL)
{
    //if ( ! preg_match('#^(\w+:)?//#i', $uri))
    //{
        $uri = site_url('?&route=' . $uri);
    //}

    // IIS environment likely? Use 'refresh' for better compatibility
    if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
    {
        $method = 'refresh';
    }
    elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
    {
        if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
        {
            $code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
                ? 303   // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
                : 307;
        }
        else
        {
            $code = 302;
        }
    }

    switch ($method)
    {
        case 'refresh':
            header('Refresh:0;url='.$uri);
            break;
        default:
            header('Location: '.$uri, TRUE, $code);
            break;
    }
    exit;
}
}

.htaccess

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Update:

Version Of Codeigniter Using 3

In my config I now use

Tried With

$config['uri_protocol'] = 'REQUEST_URI';

And

$config['uri_protocol'] = 'QUERY_STRING';

But then now saying page not found?

Page is there though.

Also "common" would be folder

And "dashboard" would be controller

All controllers have the first letter as upper case Dashboard.php

Note: I have tried to enable query strings on the config.php but does not work way I am after.

First steps I had to do to solve it was

In routes.php

$get_route_method = 'route=';
$route[$get_route_method . 'common/dashboard'] = "common/dashboard/index";

Seems to work fine now when I manually added route in application/config/route.php

Then in MY_url_helper in application/helper.php $uri = site_url('index.php?route=' . $uri);

<?php

if ( ! function_exists('redirect'))
{
function redirect($uri = '', $method = 'auto', $code = NULL)
{
    if ( ! preg_match('#^(\w+:)?//#i', $uri))
    {
        $uri = site_url('index.php?route=' . $uri);
    }

    // IIS environment likely? Use 'refresh' for better compatibility
    if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
    {
        $method = 'refresh';
    }
    elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
    {
        if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
        {
            $code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
                ? 303   // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
                : 307;
        }
        else
        {
            $code = 302;
        }
    }

    switch ($method)
    {
        case 'refresh':
            header('Refresh:0;url='.$uri);
            break;
        default:
            header('Location: '.$uri, TRUE, $code);
            break;
    }
    exit;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM