douyue8364 2015-11-02 15:55
浏览 211
已采纳

为什么你应该在相对链接中使用base_url()(Codeigniter等)

Why in relative links I should always use syntax base_url+link in CodeIgniter? For example:

<a href="<?php echo base_url()?>link22.php">

instead of

<a href="/link22.php'>

when I've got declared <base href="<?php echo base_url()?>"> in HTML head? What are the benefits?

  • 写回答

3条回答 默认 最新

  • douzong5057 2015-11-03 04:30
    关注

    Good question.

    Short words (used in below)

    CI - Codeigniter


    Why you should use base_url()??

    base_url() is a the function which determine your project destination which act as $_SERVER['REQUEST_URI'] in php. This will know where your actual project contains.


    Where we can modify/define base_url()??

    Path - application/config/
    File name - config.php
    

    Can We just use base_url() function?

    No. You can't. In CI they having some libraries and helper which help us to use CI more friendly. So to base_url() there is an helper file call url.

    How to load it??

    Path - application/config/
    File name - autoload.php
    

    there is function like this $autoload['helper'] = array();. So add the helper to that

    $autoload['helper'] = array('url');
    

    Is there any method to modify/define base_url()??

    Yes. There are two methods you can define base_url()

    Method 01

    $config['base_url'] = '';
    

    Method 02

    $config['base_url'] = 'http://localhost/foldername/';
    

    Difference between Method 01(M1) and Method 02(M2)

    M1 - When we keep empty base_url it will automatically determine your project scope. So when ever you can't find path or having some doubt this is recommend.
    M2 - There is no difference between M1, In here you know path. So can define it.

    Which method is good/recommended?

    As my suggestion is Method 01 is the best.


    Why we use echo base_url() instead of just base_url()

    base_url() is an variable which pre+defined(Codeigniter+User) in CI framework, which we can use entire project. So to show content of variable in php, we use echo. In here also same theory comes to this.


    Why this <a href="/link22.php'> will not work

    Codeigniter Framework build with MVC Structure. (MVC stand for Model View Controller.)

    So when open your project it will call to Controller, then if there any incoming values from Database. It will get that from the Model, Then it will load relevant view which mentioned in Controller.

    If this link22.php is some related file to your project you have to place that inside View folder.


    Can I include php file to another view

    Yes. you can. For that you have to use APPPATH.

    Example - Assume link22.php is inside view/links/ So you have to use

    APPPATH.'view/links/link22.php'
    

    Some Usefull links

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

报告相同问题?

悬赏问题

  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题