简体   繁体   English

使用get方法自定义URL Helper重定向

[英]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 在我的codeigniter 应用程序/帮助程序/ MY_url_helper上,我希望能够添加'?&route=' . $uri '?&route=' . $uri I would like to be able to use my '?&route=' after the index.php but before the first folder / function. '?&route=' . $uri我希望能够在index.php之后但在第一个文件夹/函数之前使用我的'?&route='

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 '?&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? '?&route=' . $uri但是每次都让它仍然重定向到页面每次,即使我点击链接的网址更改但页面保持不变?

redirect('common/dashboard');

Url http://localhost/project/admin/?&route=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 的.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 Codeigniter版本使用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 所有控制器都将第一个字母作为大写Dashboard.php

Note: I have tried to enable query strings on the config.php but does not work way I am after. 注意:我已经尝试在config.php上启用查询字符串 ,但是我没有按照我的方式工作。

First steps I had to do to solve it was 我必须做的第一步就是解决它

In routes.php 在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 当我在application / config / route.php中手动添加路由时,似乎工作正常

Then in MY_url_helper in application/helper.php $uri = site_url('index.php?route=' . $uri); 然后在app / helper.php中的MY_url_helper $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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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