doujinai2183 2017-05-23 01:26
浏览 41
已采纳

CodeIgniter 3.x - 使用用户输入重新加载具有不同CSS文件的视图

Problem:

I created a static page view (in its own view folder) that demos the new Sakura.css framework. The framework comes with four CSS themes. I want to be able to select a theme from a list of links and reload the same view with the associated CSS theme file via the controller. I also need the view to default to the 'standard.css' theme. Finally, I'd like to avoid JavaScript/jQuery solutions.

I'm struggling to grasp the relationship between controller function names, custom routing, and the URI. I think I'm at the brink of an 'aha' moment, and finding a solution this issue out is definitely going to help.

Current Code:

Note: I've also set .htaccess to remove index.php from the URL.

views/sakura/index.php

<!DOCTYPE html>

<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title>Sakura Demo</title>

    <link rel="stylesheet" href="<?php echo asset_url() ;?>css/<?php echo $theme; ?>.css" media="screen" />
</head>
<body>
<header>
    <nav>
        <ul>
            <li><a <?php if (isset($theme) && ($theme = 'standard')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/standard'); ?>">Standard</a></li>
            <li><a <?php if (isset($theme) && ($theme = 'dark')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/dark'); ?>">Dark</a></li>
            <li><a <?php if (isset($theme) && ($theme = 'earthly')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/earthly'); ?>">Earthly</a></li>
            <li><a <?php if (isset($theme) && ($theme = 'vader')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/vader'); ?>">Vader</a></li>
        </ul>
    </nav>
</header>

<main>
    <h1>Sakura.css Demo</h1>
    <p>Sakura.css is a minimal css framework/theme that can be dropped into any project for an instantaneous modern-looking website.</p>
</main>

<footer>
    <p>Footer Text</p>
</footer>
</body>
</html>

controllers/Sakura.php

<?php
class Sakura extends CI_Controller {

    public function theme($theme)
    {
        $data['theme'] = array(
            'standard' => "standard",
            'dark' => "dark",
            'earthly' => "earthly",
            'vader' => "vader"
        );

        $this->load->view('sakura/theme', $data);
    }
}

config/routes.php

<?php
$route['sakura/(:any)'] = 'sakura/$1';
$route['sakura'] = 'sakura';
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

The only way I've been able to get the view to load at all is with this function in the Sakura.php controller:

public function index()
{
    $data['theme'] = "standard";

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

Desired URL Structure: <-- Not necessary, but would be great!

Default View: mywebsite.com/sakura/ -or- mywebsite.com/sakura/standard

Theme View: mywebsite.com/sakura/selected-theme

Final Thoughts:

Thank you all in advance for any help on this issue. I rely on the StackOverflow community often and I greatly appreciate the time and consideration that goes into solving my issues.

  • 写回答

2条回答 默认 最新

  • dsbc80836 2017-05-23 01:44
    关注

    Just a guess, but try something like...?

    Controller

    $css = $this->uri->segment(2);
    $allowed_input = array('standard', 'dark', 'earthly', 'vader');
    if ((isset($css)) && (!in_array($css, $allowed_input))) {
        echo 'Filthy human!';
    }
    
    $data['css'] = $css; // You can probably just do this above, it's just harder to type.
    

    View

    <link rel="stylesheet" href="<?php echo asset_url() ;?>css/<?php echo (!isset($css) ? 'standard' : $css);?>.css" media="screen" />
    

    Route

    Change the first line to:

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

    Then mywebsite.com/sakura/standard should work, but if the "standard" bit isn't there, it'll load the standard by default. Threw in the in_array() bit as a small sanity check, but you'll probably want to be more thorough in your validation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件