在路由URL Codeigniter中添加参数

时间:2016-09-22 08:29:43

标签: codeigniter routing url-routing

我在路由和参数方面遇到了一些问题。

我在routes.php中有这样的路由:

$route['register/(:any)'] = 'member/register/$1';

在我的控制器中我喜欢这个:

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

class Register extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Page_model');
        $this->load->model('member_model');
        $this->load->model('login_model');

        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->helper('form');

    }

    public function index()
    {
        $slug = $this->uri->segment(1);
        $type = $this->uri->segment(2);

        var_dump($type);
        exit();

        if ($slug != NULL) 
        {
            $data['page'] = $this->Page_model->get_page($slug);

            if (empty($data['page'])) 
            {
                // show_404();
                $data['page'] = new stdClass();
                $data['page']->page_template = 'forofor';
                $data['page']->title = 'Page Not Found';
                $data['page']->meta_description = 'Page Not Found';
                $data['page']->meta_keywords = 'Page Not Found';
            }
            else
            {
                $data['slider'] = $this->Page_model->get_slider($data['page']->page_id);
            }
        }
        else
        {
            $data['page'] = $this->Page_model->get_page('home');
            $data['slider'] = $this->Page_model->get_slider($data['page']->page_id);
        }

        $data['head_title'] = $data['page']->title;

        // load all settings and data
        $data['settings'] = $this->Page_model->get_settings();
        $data['gallery'] = $this->Page_model->get_gallery();

        $data['meta'] = 
            array(
                array(
                    'name' => 'description', 
                    'content' => $data['page']->meta_description
                ),
                array(
                    'name' => 'keywords',
                    'content' => $data['page']->meta_keywords
                )
            );

        // $data['meta'] = $this->meta;

        $data['menu'] = $this->Page_model->get_menu('frontend','header');
        $data['settings'] = $this->Page_model->get_settings();
        $data['notice'] = $this->session->flashdata('notice');

        // $this->load->view('frontend/template/index_full', $data);

        $this->load->view('frontend/template/head', $data);
        $this->load->view('frontend/template/pre_header');
        $this->load->view('frontend/template/header');
        $this->load->view('frontend/template/modal');
        //$this->load->view('frontend/template/modal_registration');
        /*if ($page[0]->page_template != 'forofor' && $data['page']->slider != 0) 
        {
            $this->load->view('frontend/template/slider');
        }*/
        if ($slug != 'home' && $slug != NULL) 
        {
            //$this->load->view('member/'.$data['page']->page_template);
            $this->load->view('member/register');
        }
        else
        {
            $this->load->view('frontend/template/slider');
            $this->load->view('frontend/page_template/homepage');
        }
        $this->load->view('frontend/template/pre_footer');
        $this->load->view('frontend/template/footer');
        $this->load->view('frontend/template/js');
        $this->load->view('frontend/template/closing_body_html');
    }
}

但如果我在浏览器中输入路线,则会给我404找不到页面

这是我的路线:

127.0.0.1/project/register/buyer

127.0.0.1/project/是我的基本网址

任何人都知道为什么会这样?

谢谢。

2 个答案:

答案 0 :(得分:5)

您的路由错误。根据您的帖子,您正在设置Member控制器然后注册方法。

尝试以下代码

$route['register/(:any)'] = 'register/index/$1';
$route['register/(:any)/(:any)'] = 'register/index/$1/$2'; // Optional

第二个选项只是可选的:

答案 1 :(得分:3)

这是路线在Codeigniter中的工作方式

  

通常,URL字符串与之间存在一对一的关系   其相应的控制器类/方法。 URI中的段   通常遵循这种模式:

     

example.com/class/function/id/但是,在某些情况下,您可以   想要重新映射这种关系,以便不同的类/方法可以   被调用而不是与URL对应的那个。

     

例如,假设您希望您的网址具有此原型:

     

example.com/product/1/ example.com/product/2/ example.com/product/3/   example.com/product/4/通常,URL的第二段是   为方法名称保留,但在上面的示例中,它具有   产品ID。为了解决这个问题,CodeIgniter允许您重新映射   URI处理程序。

参考:https://www.codeigniter.com/userguide3/general/routing.html

因此,路线遵循此语法

$route['route_url'] = 'controller/method/$paramater';

所以,你的路线将是

让我知道您的疑问

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