php codeigniter 链接到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18387975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:28:01  来源:igfitidea点击:

codeigniter linking to another page

phpcodeigniter

提问by hasib Hasan

i am new to codeigniter framework. i am having problem with href link. in my home page i have some menu, that goes to different page. for example in normal php if i want to go Sell Books page then i just put sellBook.php in href link. now in codeigniter how can i do this. do i need write something in controller ?

我是 codeigniter 框架的新手。我在使用 href 链接时遇到问题。在我的主页上,我有一些菜单,可以转到不同的页面。例如,在普通的 php 中,如果我想去卖书页面,那么我只需将 sellBook.php 放在 href 链接中。现在在 codeigniter 中我该怎么做。我需要在控制器中写一些东西吗?

<li ><a href="http://www.studenthint.com/">Home </a> </li>
<li><a href="">Buy Books</a> </li>
<li><a href="sellBook.php">Sell Books</a> </li>
<li><a href="#">Books on Demand</a> </li>
<li><a href="#">Request a Book</a> </li>
<li><a href="#">About Us</a> </li>

回答by itsme

as per MVC pattern each of your url is a controller so:

根据 MVC 模式,您的每个 url 都是一个控制器,因此:

if you, for example, want to link to

例如,如果您想链接到

http://www.site.com/users

so the controller will look like:

所以控制器看起来像:

class Users extends CI_Controller{
  function index(){
  //do somenthing here
  }
  function list(){
  //list your users
 }
}

then in your views linking to that controller is simple:

然后在您的视图中链接到该控制器很简单:

    <a href="<?php echo site_url('users'); ?>">this will link to your users controller and index() method</a>
<a href="<?php echo site_url('users/list'); ?>">this will link to your users controller and list() method</a>

the site_url(); method will helps you finding the right link both if you are using index.php or not in your urls

site_url(); 方法将帮助您找到正确的链接,无论您是否在您的网址中使用 index.php

回答by Muhammed Safeer

<a href="<?php echo base_url() ?>controller_name/function_name">

回答by karthik

<a href="<?php echo base_url('Customer/Settings');?>">Account Details</a>

You can use like this

你可以这样使用

回答by Rht Ghodadra

<li><a href="<?php echo site_url('/') . 'controller_name/function_name' ?>">Home</a></li>
<li><a href="<?php echo site_url('/') . 'controller_name/function_name' ?>">Buy Books</a> </li>
<li><a href="<?php echo site_url('/') . 'controller_name/function_name' ?>">Sell Books</a> </li>
<li><a href="<?php echo site_url('/') . 'controller_name/function_name' ?>">Books on Demand</a> </li>
<li><a href="<?php echo site_url('/') . 'controller_name/function_name' ?>">Request a Book</a> </li>
<li><a href="<?php echo site_url('/') . 'controller_name/function_name' ?>">About Us</a> </li>

回答by Technoh

If you are using mod_rewrite to remove the index.phpfrom the URL, you can write your URL as href="/sellbook"otherwise you will have to include it, such as href="/index.php/sellbook".

如果您使用 mod_rewriteindex.php从 URL 中删除,则可以编写您的 URL,href="/sellbook"否则您将必须包含它,例如href="/index.php/sellbook".

This assumes, of course, that you have a properly configured route name sellbook. See http://ellislab.com/codeigniter/user-guide/general/routing.htmlfor details on how to achieve this.

当然,这假设您有一个正确配置的路由名称sellbook。有关如何实现这一点的详细信息,请参阅http://ellislab.com/codeigniter/user-guide/general/routing.html

回答by Zbigniew Jasek

If the links are to other pages on your site, then I think your best bet will be to use the url helper, assuming you're using the standard seo friendly format and not query strings. This way, you don't have to take into consideration whether you'll be using .htaccess or not. For example:

如果链接指向您网站上的其他页面,那么我认为您最好的选择是使用 url 助手,假设您使用的是标准的 seo 友好格式而不是查询字符串。这样,您就不必考虑是否要使用 .htaccess。例如:

echo anchor('your_controller_name/your_function_name/param_1/param_2', 'Sell Books', 'title="Sell Books"');
// or for your home page
echo anchor('/', 'Home', "title='Home'");

If it's an external link, you can use the same function or just a plain html tag:

如果是外部链接,您可以使用相同的功能或只是一个普通的 html 标签:

echo anchor('http://google.com', 'Google Me', 'title="Google Me"');
// OR
<a href="http://google.com" title="Google Me" >Google Me</a>

Note: make sure you load your url_helper in your controller if you'll be using the anchor function:

注意:如果您将使用锚点功能,请确保在您的控制器中加载您的 url_helper:

$this->load->helper('url')
// or add to autoload.php

You can also do it the way @sbaaaang suggested.

您也可以按照@sbaaaang 建议的方式进行操作。

回答by Sonali

//use
site_urlin link with controller name and method and

//使用
site_url与控制器名称和方法的链接以及

$this->load->view('page_name');