Now a days one of the big issue you have to focus that to High speed your web site performance. I have read a nice article” Performance tips for web applications” where focus the performance tips of web application. These tips can easily apply by codegIniter . Let see some of the tips below.
Some of the techniques are :
- Enable GZIP Components.
- Make Fewer Http Request .
- Adds an Expires Header.
- Minify JavaScript.
Enable GZIP Component :
In the system/application/config.ph
change the config variable true
1: $config['compress_output'] = TRUE;
Make Fewer HTTP Request :
1. Caching the site.
Each time file loads from your web server, it generates an http request. So you can reduce number of http requests by caching some contents which are almost static or changes very rarely. if these contents are loaded from browser cache, there will be no request for them over HTTP.
So you can easily caching your web Pages in the CodeIgniter :
Enabling Caching
To enable caching, put the following tag in any of your controller functions:
1: $this->output->cache(n);
Where n is the number of minutes you wish the page to remain cached between refreshes.
The above tag can go anywhere within a function. It is not affected by the order that it appears, so place it wherever it seems most logical to you. Once the tag is in place, your pages will begin being cached.
To set data you can do it by the
1: $this->output->set_output($data);
Also you can manually set header :
$this->output->set_header();
Permits you to manually set server headers, which the output class will send for you when outputting the final rendered display. Example:
1: $this->output->set_header("HTTP/1.0 200 OK");
2: $this->output->set_header("HTTP/1.1 200 OK");
3: $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
4: $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
5: $this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
6: $this->output->set_header("Pragma: no-cache");
2. join multiple css or js file into single css or js file .
It can be done in many ways . But in the CodeIgniter forum i find some ways to do that . You can check out here. The below code is the forum message :
Following is the controller to join multiple cs or jss
file in to one, and hence making fewer server request to speed up the things little bit.Just add following in to your controllers. save as file.php Set the path of your
js and css files accordingly.1:
2: class File extends Controller
3: {
4: function File()
5: {
6: parent::Controller();
7: /*Set your own path here*/
8: $this->_path = "./public/";
9: }
10: function index()
11: {
12: echo "";
13: }
14: function js()
15: {
16: $segs = $this->uri->segment_array();
17: foreach ($segs as $segment)
18: {
19: $filepath = $this->_path.$segment.'.js';
20: if(file_exists($filepath))
21: {
22: readfile($filepath);
23: }
24: }
25: }
26: function css()
27: {
28: $segs = $this->uri->segment_array();
29: foreach ($segs as $segment)
30: {
31: $filepath = $this->_path.$segment.'.css';
32: if(file_exists($filepath))
33: {
34: readfile($filepath);
35: }
36: }
37: }
38: }
39: ?>
Now you can add any number of files with just single line of code,and with single
request.
For e.g. If you want to include two css file , foo.css and bar.css , the code is ,1:
'stylesheet' type='text/css' media='all' href='< ?php echo site_url("file/css/foo/bar");?>' />
The alternative of that one is :
got a script from somewhere that gzips the css/js, caches the compilation of the files etc. Since I generally use jQuery I my controller also has the function jQuery. I can now do
This redirects to a similar function as you have but a bit shorter.
Adds an Expires Header:
you can add expire header by the Ci library output class.
Minify JavaScript:
You can minify javascript by packed, Minify.
Không có nhận xét nào:
Đăng nhận xét