【问题标题】:Remove controller name from url using codeigniter使用 codeigniter 从 url 中删除控制器名称
【发布时间】:2018-01-10 15:09:35
【问题描述】:

我想从 url 中删除我们的欢迎控制器 如何使用 codegniter 删除索引

$route['^(:any)(/:any)?$'] = "welcome/$0";

但我的链接是

<?php echo site_url('welcome/about'); ?>
<?php echo site_url('welcome/index'); ?>
<?php echo site_url('welcome/contact'); ?>

当点击此链接时,网址是相同的欢迎/联系方式,并且出现错误页面我的 $route 仅在点击任何网址 localhost/mydomain/welcome/contact 时有效 localhost/mydomain/index

【问题讨论】:

  • 当你指向一个索引函数时,你不需要包含它 &lt;?php echo site_url('welcome'); ?&gt; 而不是 &lt;?php echo site_url('welcome/index'); ?&gt;

标签: php codeigniter


【解决方案1】:

如果您只想从 URL 中删除 'welcome'

你可以这样做:

$route['(:any)'] = "welcome/$1"

其他:

您可以在config/routes.php 中定义自定义路由 - 例如:

$route['about'] = 'name_controller/about';

Then, http://example.com/about
goes to http://example.com/name_controller/about

有关详细信息,请参阅 CI 论坛中的 Hiding the controller’s method name in the URL?

【讨论】:

    【解决方案2】:

    您可以在config/routes.php添加路线

    例如:

    $route['about'] = 'welcome/about';
    $route['index'] = 'welcome/index';
    $route['contact'] = 'welcome/contact';
    

    您可以使用 Like :

    localhost/mydomain/about
    localhost/mydomain/index
    localhost/mydomain/contact
    

    请设置您的网站网址,如:

    <a href="<?= base_url() ?>about">Abouts<a/>
    <a href="<?= base_url() ?>index">Index<a/>
    <a href="<?= base_url() ?>contact">Contact<a/>
    

    【讨论】:

    猜你喜欢
    • 2011-08-04
    • 2014-12-16
    • 2011-09-05
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    相关资源
    最近更新 更多