【问题标题】:Codeigniter routing to an href idCodeigniter 路由到 href id
【发布时间】:2012-05-11 14:32:37
【问题描述】:

我做了一个单页设计,并希望将 somesite.com/contact 路由到 somesite.com/index.php#contact。我正在使用 href id 和 div id。但我所做的所有路线似乎都没有奏效。

例子:

  • $route['contact'] = "pagescontroller/index#contact";

  • $route['contact'] = "base_url('#contact')";

我怎样才能做到这一点?

提前致谢!

【问题讨论】:

  • htaccess 我的朋友,htaccess...
  • 我在htacces方面的技能非常有限,我不能为此使用CI路由吗?

标签: codeigniter url hyperlink


【解决方案1】:

您需要使用#,以便浏览器知道您将转到页面的一部分。如果您真的想删除它,可以将其用作用于显示页面的控制器方法的参数,并使用 javascript 转到页面上的特定 ID。
我不会这样做,因为每次单击链接时您都会重新加载页面。 (除非您也使用 javascript 来抑制它并转到相关 ID。)

但是,您可以在 .htaccess 中使用 mod_rewrite 从您的 url 中删除 index.php。所以你的网址看起来像http://example.com/#about

我的 codeigniter .htaccess 文件是这样的:

<IfModule mod_rewrite.c>

    RewriteEngine On

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

【讨论】:

  • 等等,我已经在这个页面上放了一个联系表,当它发送时我会看到一条消息,消息就在那里,但是联系页面在底部,刷新页面是在它的顶部。这就是为什么我想将 index.php#contact url 覆盖到 /contact 以便我可以返回到确切的页面。
  • 您可以返回example.com/#contact,这样您就可以到达正确的高度。否则,您将不得不使用一些 javascript 来达到正确的高度。您还可以在 example.com/contact 处理您的表单,将您的消息存储在 session-flash-data 中,重定向到 example.com/#contact 并显示您的消息。
  • 也许您可以使用 ajax 发送表单并显示消息,这样您也可以保持正确的高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2011-11-29
  • 2010-11-20
  • 2011-07-08
相关资源
最近更新 更多