【问题标题】:Codeigniter Route url parents page and child pageCodeigniter Route url 父页面和子页面
【发布时间】:2014-05-14 15:50:13
【问题描述】:

编辑:我是新的 Codeigniter 我不是如何使用 Codeigniter 路由。我创建联系我们页面和地图页面。地图页面是联系我们页面的子页面。

表格名称:页数

id  label       link            parent
1   Contact Us  contact-us      0
3   About Us    about-us        0
2   Map         map             1

这是我的控制器

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

class Page extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('page_model');
    }
    public function index()
    {   

        $data['getAllPage'] = $this->page_model->getAllPages(); 

        $this->load->view('page_listing',$data);

    }
    public function view($id) {
        $data['single_page'] = $this->page_model->displaySinglePage($id);
        $this->load->view('single_page',$data);
    }
}

在 routes.php 我已经把 $route['(:any)'] = "page/view/$1";

当我输入 url "http://mytest.dev/contact-us/" 或 "http://mytest.dev/about-us/" 时,它会显示正确的联系页面内容,但我输入 "http://mytest.dev/contact-us/map" 它仍然显示联系页面的内容。

当我输入“http://mytest.dev/contact-us/map”时我想要什么,它应该显示地图页面的内容

提前致谢。

【问题讨论】:

  • 我假设您正在谈论在 CI 中设置静态页面...如果是这样,请发布您为静态页面设置的控制器逻辑。
  • 动态页面不是静态页面
  • 告诉我们控制器名称及其方法名称。从那里可以很容易地配置路线。
  • 我有添加控制器代码

标签: php codeigniter url-routing


【解决方案1】:

请尝试代码也许可以帮助

// Parents and Child page.
$route['page/(:any)/(:num)'] = "page/view/$1/$2";

// For Main Home Page.
$route['(:any)'] = "page/view/$1";

【讨论】:

    【解决方案2】:

    我认为以下路线应该可行

    $route['contact-us'] = 'page/view/$1';
    
    $route['contact-us/map'] = 'page/view/$2';
    

    当您在视图方法中回显 $id 时。

    "http://mytest.dev/contact-us/" 将打印 $1

    http://mytest.dev/contact-us/map”将打印 $2

    希望这个解决方案会有所帮助。

    【讨论】:

    • 感谢您的回复。你的 anwser 是静态 url 我想要的 url 是动态的。抱歉没说清楚
    • 那么,你要指向多少个URLs来查看方法呢?只是联系我们和关于我们吗?还是还有更多?
    【解决方案3】:

    我的网站有一个主“事件”页面和一个“事件/日历”页面。将页面创建为单独的视图,然后在主控制器中为每个子页面创建一个函数。这是我的事件控制器。如果你想要一个动态页面,你可以在函数中添加适当的代码。

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Events extends CI_Controller {
    
        public function index()
        {
            $this->load->view('inc/header.php');
            $this->load->view('events_view');
            $this->load->view('inc/footer.php');
        }
    
        public function calendar()
        {
            $this->load->view('inc/header.php');
            $this->load->view('calendar_view');
            $this->load->view('inc/footer.php');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多