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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历