【问题标题】:Codeigniter routing permalinksCodeigniter 路由永久链接
【发布时间】:2017-10-27 16:50:12
【问题描述】:

好的,所以我使用位于我的视图文件夹中的 index.php 作为我的主视图。我在其中加载了所有其他视图(正如您从下面的代码中看到的那样)。

现在,当我创建一个类似 about/(:any) 的路由时,它不会加载我的任何 CSS 或类似的......

路线

$route['about']                     =  'about'; // works fine
$route['about/(:any)']              =  'about/get_specific_about/$1'; //problematic one

现在这是我的 index.php

<!DOCTYPE HTML>

<html>

    <head>
        <link rel="stylesheet" type="text/css" href="public/css/partials/header.css" />
        <link rel="stylesheet" type="text/css" href="public/css/partials/footer.css" />
        <link rel="stylesheet" type="text/css" href="public/css/<?php echo $meta['css']; ?>.css" />
    </head>

    <body>
        <?php $this->load->view("partials/header");         ?>
        <?php $this->load->view($meta['page']);             ?>
        <?php $this->load->view("partials/footer");         ?>

    </body>


</html>

现在每当我使用带有 (:any) 的路由时,代码都会加载视图,但它无法找到例如 footer.css 或 header.css .. 如果我使用例如 ../public/css /header.css 然后它会...

关于控制器的代码

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

class About extends CI_Controller {

    private $data = [
        'meta'  => [
            'page'          =>  "pages/about",
            'css'           =>  "about",
            'description'   =>  "test"
        ],

    ];

    public function index()
    {

        $this->load->helper('html');

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

    public function get_specific_about($user) {
        $tmp_data                           =   [];
        $tmp_data['meta']['css']            =   "profiles/".$user;
        $tmp_data['meta']['page']           =   "profiles/".$user;
        $tmp_data['meta']['description']    =   "test";
        $tmp_data['meta']['extended']       =   true;

        $this->load->view('index', $tmp_data);
    }

}

【问题讨论】:

  • 我认为问题不在于路线;这是您使用资产的相对路径。尝试通过在它们前面加上 $this-&gt;config-&gt;site_url() 将资产路径锚定到根目录

标签: php codeigniter


【解决方案1】:

对 href 使用绝对 URL 链接

<head>
    <link rel="stylesheet" type="text/css" 
          href="<?php echo base_url('public/css/partials/header.css'); ?>>" />
    <link rel="stylesheet" type="text/css" 
          href="<?php echo base_url('public/css/partials/footer.css'); ?>" />
    <link rel="stylesheet" type="text/css" 
          href="<?php echo base_url("public/css/{$meta['css']}.css"); ?>" />
</head>

如果你没有使用.htaccess' to rewrite URLs withoutindex.phpthen substitutesite_urlforbase_url`。

【讨论】:

    猜你喜欢
    • 2017-01-08
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多