将网址参数发送到codeigniter中的站点

时间:2018-09-19 11:21:31

标签: codeigniter

我在该文件夹中有一个文件夹“模块”,即站点“ manage.php”。

控制器和模型正常工作,因此manage.php站点的视图很好。

现在,我想包含一个带有参数的链接,以重新加载页面并显示其他内容。

我尝试对链接执行此操作:admin_url('module / manage / 1 <-这是我的参数)

结果我得到了这个网址:... admin / module / manage / 1

它看起来正确,但是如果我单击此链接,则会收到错误404-找不到页面。

我做错了什么或我忘了做什么?

非常感谢您的帮助。

config.php文件(系统/核心)

    <?php
    /**
     * CodeIgniter

...
class CI_Config {

    /**
     * List of all loaded config values
     *
     * @var array
     */
    public $config = array();

    /**
     * List of all loaded config files
     *
     * @var array
     */
    public $is_loaded = array();

    /**
     * List of paths to search when trying to load a config file.
     *
     * @used-by CI_Loader
     * @var     array
     */
    public $_config_paths = array(APPPATH);

    // --------------------------------------------------------------------

    /**
     * Class constructor
     *
     * Sets the $config data from the primary config.php file as a class variable.
     *
     * @return  void
     */
    public function __construct()
    {
        $this->config =& get_config();

        // Set the base_url automatically if none was provided
        if (empty($this->config['base_url']))
        {
            if (isset($_SERVER['SERVER_ADDR']))
            {
                if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE)
                {
                    $server_addr = '['.$_SERVER['SERVER_ADDR'].']';
                }
                else
                {
                    $server_addr = $_SERVER['SERVER_ADDR'];
                }

                $base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
                    .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
            }
            else
            {
                $base_url = 'http://localhost/';
            }

            $this->set_item('base_url', $base_url);
        }

        log_message('info', 'Config Class Initialized');
    }

    // --------------------------------------------------------------------

    /**
     * Load Config File
     *
     * @param   string  $file           Configuration file name
     * @param   bool    $use_sections       Whether configuration values should be loaded into their own section
     * @param   bool    $fail_gracefully    Whether to just return FALSE or display an error message
     * @return  bool    TRUE if the file was loaded correctly or FALSE on failure
     */
    public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
    {
        $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
        $loaded = FALSE;

        foreach ($this->_config_paths as $path)
        {
            foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location)
            {
                $file_path = $path.'config/'.$location.'.php';
                if (in_array($file_path, $this->is_loaded, TRUE))
                {
                    return TRUE;
                }

                if ( ! file_exists($file_path))
                {
                    continue;
                }

                include($file_path);

                if ( ! isset($config) OR ! is_array($config))
                {
                    if ($fail_gracefully === TRUE)
                    {
                        return FALSE;
                    }

                    show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
                }

                if ($use_sections === TRUE)
                {
                    $this->config[$file] = isset($this->config[$file])
                        ? array_merge($this->config[$file], $config)
                        : $config;
                }
                else
                {
                    $this->config = array_merge($this->config, $config);
                }

                $this->is_loaded[] = $file_path;
                $config = NULL;
                $loaded = TRUE;
                log_message('debug', 'Config file loaded: '.$file_path);
            }
        }

        if ($loaded === TRUE)
        {
            return TRUE;
        }
        elseif ($fail_gracefully === TRUE)
        {
            return FALSE;
        }

        show_error('The configuration file '.$file.'.php does not exist.');
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a config file item
     *
     * @param   string  $item   Config item name
     * @param   string  $index  Index name
     * @return  string|null The configuration item or NULL if the item doesn't exist
     */
    public function item($item, $index = '')
    {
        if ($index == '')
        {
            return isset($this->config[$item]) ? $this->config[$item] : NULL;
        }

        return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a config file item with slash appended (if not empty)
     *
     * @param   string      $item   Config item name
     * @return  string|null The configuration item or NULL if the item doesn't exist
     */
    public function slash_item($item)
    {
        if ( ! isset($this->config[$item]))
        {
            return NULL;
        }
        elseif (trim($this->config[$item]) === '')
        {
            return '';
        }

        return rtrim($this->config[$item], '/').'/';
    }

    // --------------------------------------------------------------------

    /**
     * Site URL
     *
     * Returns base_url . index_page [. uri_string]
     *
     * @uses    CI_Config::_uri_string()
     *
     * @param   string|string[] $uri    URI string or an array of segments
     * @param   string  $protocol
     * @return  string
     */
    public function site_url($uri = '', $protocol = NULL)
    {
        $base_url = $this->slash_item('base_url');

        if (isset($protocol))
        {
            // For protocol-relative links
            if ($protocol === '')
            {
                $base_url = substr($base_url, strpos($base_url, '//'));
            }
            else
            {
                $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
            }
        }

        if (empty($uri))
        {
            return $base_url.$this->item('index_page');
        }

        $uri = $this->_uri_string($uri);

        if ($this->item('enable_query_strings') === FALSE)
        {
            $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';

            if ($suffix !== '')
            {
                if (($offset = strpos($uri, '?')) !== FALSE)
                {
                    $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
                }
                else
                {
                    $uri .= $suffix;
                }
            }

            return $base_url.$this->slash_item('index_page').$uri;
        }
        elseif (strpos($uri, '?') === FALSE)
        {
            $uri = '?'.$uri;
        }

        return $base_url.$this->item('index_page').$uri;
    }

    // -------------------------------------------------------------

    /**
     * Base URL
     *
     * Returns base_url [. uri_string]
     *
     * @uses    CI_Config::_uri_string()
     *
     * @param   string|string[] $uri    URI string or an array of segments
     * @param   string  $protocol
     * @return  string
     */
    public function base_url($uri = '', $protocol = NULL)
    {
        $base_url = $this->slash_item('base_url');

        if (isset($protocol))
        {
            // For protocol-relative links
            if ($protocol === '')
            {
                $base_url = substr($base_url, strpos($base_url, '//'));
            }
            else
            {
                $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
            }
        }

        return $base_url.$this->_uri_string($uri);
    }

    // -------------------------------------------------------------

    /**
     * Build URI string
     *
     * @used-by CI_Config::site_url()
     * @used-by CI_Config::base_url()
     *
     * @param   string|string[] $uri    URI string or an array of segments
     * @return  string
     */
    protected function _uri_string($uri)
    {
        if ($this->item('enable_query_strings') === FALSE)
        {
            is_array($uri) && $uri = implode('/', $uri);
            return ltrim($uri, '/');
        }
        elseif (is_array($uri))
        {
            return http_build_query($uri);
        }

        return $uri;
    }

    // --------------------------------------------------------------------

    /**
     * System URL
     *
     * @deprecated  3.0.0   Encourages insecure practices
     * @return  string
     */
    public function system_url()
    {
        $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
        return $this->slash_item('base_url').end($x).'/';
    }

    // --------------------------------------------------------------------

    /**
     * Set a config file item
     *
     * @param   string  $item   Config item key
     * @param   string  $value  Config item value
     * @return  void
     */
    public function set_item($item, $value)
    {
        $this->config[$item] = $value;
    }

}

routes.php(应用程序/配置)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|   example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|   http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are three reserved routes:
|
|   $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|   $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router which controller/method to use if those
| provided in the URL cannot be matched to a valid route.
|
|   $route['translate_uri_dashes'] = FALSE;
|
| This is not exactly a route, but allows you to automatically route
| controller and method names that contain dashes. '-' isn't a valid
| class or method name character, so it requires translation.
| When you set this option to TRUE, it will replace ALL dashes in the
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
|       my-controller/my-method -> my_controller/my_method
*/

$route['default_controller'] = 'clients';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['admin']  = "admin/dashboard";
// Misc controller rewrites
$route['admin/access_denied']  = "admin/misc/access_denied";
$route['admin/not_found']  = "admin/misc/not_found";

// Staff rewrites
$route['admin/profile']  = "admin/staff/profile";
$route['admin/profile/(:num)']  = "admin/staff/profile/$1";
$route['admin/tasks/view/(:any)']  = "admin/tasks/index/$1";

// Items search rewrite
$route['admin/items/search'] = 'admin/invoice_items/search';

/* Clients links and routes */
// // In case if client access directly to url without the arguments redirect to clients url
$route['/']  = "clients";

// Deprecated
$route['viewinvoice/(:num)/(:any)']  = "invoice/index/$1/$2";

// New url from version 2.0.
$route['invoice/(:num)/(:any)']  = "invoice/index/$1/$2";

// Deprecated
$route['viewestimate/(:num)/(:any)']  = "estimate/index/$1/$2";

// New url from version 2.0
$route['estimate/(:num)/(:any)']  = "estimate/index/$1/$2";

$route['subscription/(:any)']  = "subscription/index/$1";

// Deprecated
$route['viewproposal/(:num)/(:any)']  = "proposal/index/$1/$2";
// New url from version 2.0
$route['proposal/(:num)/(:any)']  = "proposal/index/$1/$2";

// Available from version 2.0
$route['contract/(:num)/(:any)']  = "contract/index/$1/$2";
$route['survey/(:num)/(:any)']  = "survey/index/$1/$2";

// Deprecated
//$route['knowledge_base']  = "knowledge_base/index";
//$route['knowledge_base/(:any)']  = "knowledge_base/index/$1";

// Available from version 2.0
$route['knowledge-base']  = "knowledge_base/index";
$route['knowledge-base/search']  = "knowledge_base/search";
$route['knowledge-base/article']  = "knowledge_base/index";
$route['knowledge-base/article/(:any)']  = "knowledge_base/article/$1";
$route['knowledge-base/category']  = "knowledge_base/index";
$route['knowledge-base/category/(:any)']  = "knowledge_base/category/$1";

// Deprecated
if(strpos($_SERVER['REQUEST_URI'],'add_kb_answer') === false) {
    $route['knowledge-base/(:any)']  = "knowledge_base/article/$1";
    $route['knowledge_base/(:any)']  = "knowledge_base/article/$1";
    $route['clients/knowledge_base/(:any)']  = "knowledge_base/article/$1";
    $route['clients/knowledge-base/(:any)']  = "knowledge_base/article/$1";
}
// $route['knowledge-base/(:any)']  = "knowledge_base/index/$1";
$route['terms-and-conditions']  = "clients/terms_and_conditions";
$route['privacy-policy']  = "clients/privacy_policy";

if(file_exists(APPPATH.'config/my_routes.php')){
    include_once(APPPATH.'config/my_routes.php');
}

1 个答案:

答案 0 :(得分:0)

如果我的理解正确,那么您的modules文件夹中有一个名为controllers的文件夹。该答案是假设您已经摆脱了URI的index.php?部分。

我们当然知道基本控制器应该看起来像这样(所以您的Manage.php应该看起来像这样:

[重要提示:控制器文件名应以大写开头并与类名匹配]

Class Manage extends CI_Controller{

    public function __construct(){
        parent::__construct();
        // add libraries/models
    }

    public function index(){
        // index logic
    }

    public function newMethod($param){
        //The method used to capture your parameter.
    }
}

如果设置正确,那么您应该可以进入routes.php并添加如下内容:

$route['admin/modules/(:num)'] = 'modules/Manage/newMethod/$1';

那么您应该可以访问www.yoursitename.com/admin/modules/#INSERT-A-NUMBER并得到想要的东西。

或者,您可以更改路由,以使其接受任何内容作为参数,而不仅仅是数字。看起来像这样:

$route['admin/modules/(:any)'] = 'modules/Manage/newMethod/$1';

希望这会有所帮助!