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

标签 php codeigniter

在我的 codeigniter application/helper/MY_url_helper 上,我希望能够添加 '?&route=' 。 $uri 我希望能够在 index.php 之后但在第一个文件夹/函数之前使用我的 '?&route='

但是每次我点击我的链接时,url 都会改变但页面保持不变?

问题:如何在我的助手上添加 '?&route=' 。 $uri 但仍然重定向到页面 目前每次即使我点击链接时 url 都会更改但页面保持不变?

redirect('common/dashboard');

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

我已经尝试过路由和同样的问题。

<?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]

更新:

使用 3 的 Codeigniter 版本

在我的配置中我现在使用

尝试过

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

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

但是现在说找不到页面?

虽然页面在那里。

"common" 也是文件夹

“仪表板”将成为 Controller

所有 Controller 首字母大写 Dashboard.php

注意:我已经尝试在 config.php 上启用查询字符串,但没有按照我想要的方式工作。

最佳答案

我必须做的第一步是解决它

在 routes.php 中

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

当我在 application/config/route.php 中手动添加路由时,现在似乎工作正常

然后在 application/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;
}
}

关于php - 使用 get 方法自定义 URL Helper 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30929820/

相关文章:

mysql - 当有新的 mysql 记录插入表中时创建 Node.js 事件监听器(用于通知提要)

codeigniter - 在 Codeigniter DBForge 迁移中创建唯一字段

php - Laravel Eloquent 在播种期间无人看守

php - 如何从字符串中删除 iframe 标签?

php - 连接名称并再次使用 concat 作为用户名?

php - ArgumentCountError-参数太少

php - 将 codeigniter 2.0 与 Datamapper 一起使用时类名冲突?

php - 如何在没有 SDO 的情况下加载架构、设置属性和输出字符串?

php - MySQL(i?) 过滤现有查询

php - 为什么添加 CodeIgniter 构造函数会产生错误 500