Auxiliary Method for codeigniter url Extension

Source: Internet
Author: User

Since I switched to Ruby, I haven't touched PHP for a while.

Today, Codeigniter is used to write something casually. I 've forgotten about what I saw before.

It is inconvenient for me to introduce style, images, and javascript at the View layer. Read the manual.
URL auxiliary function base_url () found ()

Configure base_url () as the root url of the website in config. php.
Create a public folder in the root directory of the project, and divide it into three folders: css, images, and javascript.
The base_url () must be used in the view layer.
Introduce css 1 <link rel= 'stylesheet'  type= 'text/css'  href= '<?php echo base_url("/public/css/style.css"); ?>' media= 'all' >Introduce javascript
1 <script type= 'text/javascript' src= '<?php echo base_url("/public/javascript/jquery.js"); ?>' ></script>
If the image is displayed
1 < img  src='<?php echo base_url("/public/images/hello.png");?>'/>
/Public/css/public/images/public/javascript every time
If the path below is deeper, it is inconvenient!

It suddenly feels weak. There are wood ......

Thinking ......

Since base_url () can be configured in config. php, can I configure some?
Thunder cannot hide your ears and ears. The three formats configured under base_url are identical and certainly not wrong.
1 // these is only a try 2 $config [ 'style_url' ] =  'http://localhost/new/public/css/' ; 3 $config [ 'images_url' ] =  'http://localhost/new/public/images/' ; 4 $config [ 'javascript_url' ] =  'http://localhost/new/public/javascript/' ;Note: http: // localhost/new/is the url of my project.

Refresh the page. An error is reported. The corresponding function does not exist. codeigniter implements the base_url method in the url helper function,
Our own style_url images_url javascript_url does not automatically implement the corresponding method.

What should I do?

If this is not the case, you can only extend the url auxiliary method. I found it in system/helpers /.

Url_helper.php file open search base_url

Actually, base_url () is implemented in it ()
1 if  ( ! function_exists( 'base_url' )) 2 { 3      function  base_url( $uri  '' ) 4      { 5          $CI  =& get_instance(); 6          return  $CI ->config->base_url( $uri ); 7      } 8 }
A few simple lines of code, copy the code and paste it behind the base_url function, and slightly modify it to implement my own url helper Function
1 if  ( ! function_exists( 'css_url' )) 2 { 3      function  css_url( $uri  '' ) 4      { 5          $CI  =& get_instance(); 6          return  $CI ->config->base_url( "/public/css" . $uri ); 7      } 8 }
Modify the view page and use css_url () to load the css file.
1 < link  rel = 'stylesheet'  type = 'text/css'  href='<?php echo css_url("/style.css"); ?>' media='all'>Refresh. The effect is not displayed. If not, you must have a problem with the place and check it carefully.

Then copy? How does one implement javascript image url?
The core file of the framework cannot be modified as needed !!!! See the manual for custom auxiliary methods.

Create a MY_url_helper.php file under/application/helpers /.
Add
1 <?php   if  ( ! defined( 'BASEPATH' ))  exit ( 'No direct script access allowed' );Cut out the implemented css_url function from the system file and paste it into this file.

Then implement the javascript_url image_url method again. Note that the path is modified according to your project.

Is it much easier to introduce style files after this?

However, I am still not satisfied. I still need to write a long link tag script tag.
Although I know there is zencoding, I still want to implement it using code. --! Otherwise, how can we install experts?

You can use a function in rails. Of course, php can also be used, so we can rebuild MY_url_helper.php.
The code is simple. My code is as follows:
01 if  ( ! function_exists( 'css_url' )) 02 { 03      function  css_url( $uri  '' ) 04      { 05          $CI  =& get_instance(); 06          $css_string "<link rel='stylesheet' type='text/css' href='" . $CI ->config->base_url( "/public/css" . $uri ). "' media='all'>" ; 07          return  $css_string ; 08      } 09 } 10 //--------------------------------- 11 if  ( ! function_exists( 'javascript_url' )) 12 { 13      function  javascript_url( $uri  '' ) 14      { 15          $CI  =& get_instance(); 1           $javascript_string "<script type='text/javascript' src='" .base_url( "/public/javascript" . $uri ). "'></script>" ; 1          return  $javascript_string ; 2      } 3 }
In view, we can add a short file name to a function to easily introduce css javascript.

View source Print? 1 <? php  echo css_url("/style.css"); ?> 2 //< link  rel = 'stylesheet'  type = 'text/css' 3  4 href = 'http://localhost/new/public/css/style.css'  media = 'all' > 5  6 <? php  echo javascript_url("/jquery.css"); ?> 7 //< script  type = 'text/javascript' 8  9 src = 'http://localhost/new/public/javascript/jquery-1.8.2.js' ></ script >

Very good and powerful. If you want to, you can make improvements on your own.

When using base_url (), do not forget to load the url helper function.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.