官术网_书友最值得收藏!

  • CodeIgniter 1.7
  • David Upton Jose Argudo Blanco
  • 2771字
  • 2021-04-01 13:45:29

Installing CodeIgniter

One thing you don't need is your credit card—CI is completely free!

Once your server is set up, go to the CodeIgniter site at (http://www.codeigniter.com/) and download the latest version of the framework. Version 1.7.2, the latest, it is only 2.1 MB when zipped, so it doesn't take much time to download the framework.

Unzip the folder, and install the CodeIgniter files in your website's root folder. If you are using XAMPP Lite, this is usually the htdocs folder within the xampplite folder. For WAMP it is www inside the wamp folder.

The CodeIgniter index.php file should be in the root directory. The root folder is the folder that you would point at if you navigated to the site—in this case, by accessing http://127.0.0.1. If you put CodeIgniter in some other directory you will need to point to that directory. For example, if you create a folder called codeigniter inside your WAMP installation, you will need to put http://127.0.0.1/codeigniter, in order to access your CodeIgniter installation.

Included with CI is a comprehensive user guide (in the user_guide folder), it's the same as the one on their website. You'll use this a lot. It is usually clear, try it if you get stuck, it will save your time.

When these files are on your machine, you can access them in two ways:

  • As a URL: For example, http://127.0.0.1
  • Through the normal directory path: For example, C:/xampplite/htdocs/index.php

You should be able to see the CI welcome screen by simply navigating to your URL with the browser. It's that simple! The welcome page tells you, what you are seeing is built by two file—a view and a controller.

Exploring the file structure

Once you have installed the CI files, have a look at the new directories that have been created. Understanding what different type of files and folders do is critical.

The initial files and folders we can see are:

  • system
  • user_guide
  • index.php
  • license.txt

These are our main site's files and folders, the index.php file being the most important one, it will act as the main controller and will route application requests to the required controllers. Without this file, CodeIgniter can't work.

Next in importance is the system folder. The framework's base files and libraries are present in this folder. We, as programmers, won't need to modify those files for our normal usage. However, it's important to know what those files are and what they do.

The application folder is placed inside the system folder. Our site's files and code will be put here. This separation of the system and application folders has two purposes:

  • We can update the CI framework without any trouble, as our files are separated from the framework's. As we don't make changes to CI base files updating those files won't do any harm.
  • We can have as many application folders as we need, all of them using the same system folder. That is a great advantage, because if we need to update our framework version, we will need to do it only once.

Of course, there are more files and folders. If you have opened the system folder you would have glanced through them. We are going to see them one by one. If you've ever looked at any other PHP framework, the structure will look fairly familiar. The folder structure is similar to the following:

system

  • application: This folder contains your application files. The following is the list of folders in the application folder:
    • config: This folder contains CI configuration files, such as database connection files and more.
    • controllers: This folder contains your application controller files.
    • errors: This folder contains your error documents, such as 404 error pages.
    • helpers: This folder contains helper files.
    • hooks: This folder contains files that are intended to change CI functionalities without changing CI core files. This way your changes would not be lost if you update your CI version.
    • language: This folder contains language files for your application.
    • libraries: This folder contains your own libraries and classes.
    • models: This folder contains model files.
    • views: This folder contains your application's view files.
  • cache: This folder contains cache files that will be created if you use CI cache capabilities. This directory needs write permissions in your server.
  • codeigniter: This folder contains CI framework files, you shouldn't need to edit those files.
  • database: This folder contains database connection libraries and other tools, again you will rarely be editing those files.
  • fonts: This folder contains fonts that will be used by the image library, for example, when watermarking images.
  • helpers: This folder contains CI helpers (email helper, form helper).
  • language: This folder contains language files for CI libraries. Your application's language files can be placed inside the application\language folder.
  • libraries: This folder contains CI library files, containing the classes that will help you build your application. If you create your own libraries place them inside application\libraries.
  • logs: This folder contains log files.
  • plugins: This folder contains some CI plugins. If there is no plugins folder inside application you can make it and put your plugins there.
  • scaffolding: This folder contains CI CRUD generation files.

user_guide: This folder contains help documents for CI, they are the same as those on CodeIgniter's site.

index.php: This file must be in the document root of your server so that CodeIgniter can work.

license.txt: This file is the license document, you should read it.

Note

If you are working on a Linux server remember that paths use the forward slash, that is: application/language.

Does it work?—checking our CI installation

Now that we have taken a look at our CI files and structure, we are going to check if it's working. An easy way to see if your site is working is to navigate to it using your browser. For example, if you have installed CI in a folder called codeigniter inside your wamp installation, you can see it by using the path http://127.0.0.1/codeigniter/ in your browser. You should see something similar to the following screenshot:

Does it work?—checking our CI installation

This means CI is up and running. Did you see how easy it is? We will need to make some configuration changes to adapt it to our needs, but that will be very easy.

The configuration file

Remember we were going to take two minutes to set up our site. The second minute is spent doing some basic configuration.

The config folder contains a group of files that set basic configurations for your site. Open the system/application/config/config.php file and tell the site where to find itself. The first few lines of the file should say something like:

/*
|------------------------------------------------
| Base Site URL
|------------------------------------------------
|
| URL to your Code Igniter root. Typically this
| will be your base URL, WITH a trailing slash:
|
| http://www.your-site.com/
|
*/
$config['base_url'] = "http://127.0.0.1/";
/*

Notice, how well CI files are commented! Alter the values in quotes to match your own website's root. If you have a problem, more detailed set up instructions are given in the online manual. Some functions such as anchor and base_url use this configuration, so modify it carefully.

As a basic principle, use the config.php file to store information about your site rather than scattering it around your files. In the first place, it is easier to update if it's all in one place. Second, when you transfer your site from the development server to the production server, you'll have to make only one set of changes. Lastly, many CI functions assume that certain information is to be found there.

There are other config files in the config folder. The database connection file, the routes file, and the autoload file are some of the most important files we will be changing for our application to work correctly. Should your CI installation produce a 404 error, after this basic configuration, look at line 44 of the config file:

$config['uri_protocol'] = "AUTO";

This line determines which global variable will be used to retrieve the query string. This is needed to create nice URLs and know which part of the URL is our path to the file and which part contains the variables we are passing between pages. Sometimes one of those variables is not available, though usually AUTO will work, on other occasions we will have to specify which one should be used, manually.

For example, take this URL:

http://localhost/demo/sayhello.php?to=Fred.

In my server configuration, PATH_INFO and ORIG_PATH_INFO return nothing, but the others return:

  • QUERY_STRING: to=Fred
  • REQUEST_URI: /demo/sayhello.php?to=Fred

As we can see, they work in a different way, if PATH_INFO had been active it would have returned the values before the question mark, that is:

  • PATH_INFO: /demo/sayhello.php

If the AUTO option doesn't work for you, try to change this setting with the other options, and if none of them works for you then change your index_page variable to:

$config['index_page'] = "index.php?";

The only difference is the question mark (?), which should solve your problem. As we have seen before, these methods are used to determine which part of the URL is our path to the file and which part contains the variables. For some reason the question mark (?) is not automatically appended to the URL, the reason couldn't be determined. At these times we will put the question mark manually.

Autoloading libraries, helpers, and so on

Inside the config folder you will also find the autoload.php file. This file will help you determine which libraries, helpers, and so on, should be autoloaded within your application. But at this point you may not know what libraries or helpers are. Don't worry about that, we will summarize this concept. Libraries are like classes, which contain methods that help us while working with some tasks such as, session control, database access, and a lot of other things. Helpers work in the same way but in a smaller scope.

You need to load the library first and then you can use it. Usually, to load a library you need to do something like:

$this->load->library('pagination');

Similarly for helper loading you need:

$this->load->helper('form');

After doing that you can use the library or helper. If you need to use a particular library or helper once or twice in your application there is no problem with that. But now imagine, you have to write those lines in every controller function of your site.

If you want to save some time, and forget about the repetitive task of adding the loading lines, you can put those libraries and helpers in the autoload.php file. For example, if you are using the pagination library you could add it to the autoload file this way:

$autoload['libraries'] = array('pagination');

Now you will have access to the functions of the pagination library in every function of your application, without the need to load it first. Another good candidate library to be autoloaded is the database library, you can add it in the same array (don't do it now, just take a look at how it works):

$autoload['libraries'] = array('pagination', 'database');

If your application is using a database, you will end up putting the database library in the autoload array, eliminating the necessity to load the library every time you need access to the database.

Helpers, plugins, and extra configuration files could be added to the autoload.php file the same way, in their corresponding arrays. You maybe tempted to put all libraries and helpers you will be using in this file. But remember that all the libraries and helpers you put here add to the loading time and your application will load slowly.

If your site has only a few visitors you don't need to look after these things. But as time passes by and, luckily, your site becomes popular, all these things count and you need to take everything into account.

Don't worry about this, after you have built some sites with CI, you will know which libraries, helpers, plugins, and so on should be put here and you will be doing that soon. That means CI is up and running. Did it take more than two minutes?

Mod rewrite and apache .htaccess to achieve nice URL rewrites

Earlier we saw the welcome page of CI, it is there by default, thanks to the welcome controller and welcome view. The welcome controller is the default controller (this is configured in the routes.php file of the config directory), so it is loaded on the home page of our application.

Note

This part is a bit more technical, our CI installation can work without doing this, so you can obviate it.

If we want to see it and if the default controller isn't there, we would have to put this URL:

http://127.0.0.1/codeigniter/index.php/welcome

Note the index.php in the path between the base URL and the controller we want to load. If you try to put the URL like:

http://127.0.0.1/codeigniter/welcome

You will get an error page, but this can be solved, so we can get rid of the index.php. This, we have to admit, doesn't make our URLs easy to write. The first thing that we need to do is put an .htaccess file in our application's root. Of course our server needs to support the use of these files (you can ask your hosting provider about this).

We can find a sample .htaccess file for CI on Wiki:

http://codeigniter.com/wiki/mod_rewrite/

Create a file called .htaccess in your application root (where the index.php file was located), and copy the code you found on Wiki:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php
#controller, previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /codeigniter/index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends
#the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeigniter/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>

If you cannot create the .htaccess file (Windows needs a file name to be put before the extension), then create it on a Linux server and download it, after that you will be able to edit it. For example, you can connect it to an FTP tool (like FileZilla), to another hosting you have, and create the file and download it to your PC.

After creating the file and copying the code from Wiki into it, you may need to change it to adapt to your hosting configuration. If we have to add our base URL /codeigniter at some places (it is in bold so you can recognize it), we need to change the config.php file, located at system/application/config. The only thing you need to do is change this line:

$config['index_page'] = "index.php";

To this:

$config['index_page'] = "";

After that you will be able to go to http://127.0.0.1/codeigniter/welcome and you won't need to put index.php in between your base URL and your controller. Now it is much easier to write, and for your visitors to remember, it's also better for your website's SEO.

Moving the application directory and the system directory—benefits

Here we are making the final changes to our CI installation.

Note

This part is also not necessary, but it offers a bit more security to our CI installation.

To make these changes, open the index.php file, go to the system folder, and move the application folder and all it's content to the same level as the index.php file (cut and paste the entire folder with all it's subfolders). Now put the system folder outside the document root and change the following line in index.php file from:

$system_folder = "system";

To:

$system_folder = "../CI_system";

We can change the name of the folder from system to CI_system, or whatever you want.

So now our structure will look similar to the following:

CI_system
www (or htdocs in xampp)
codeigniter
application
user_guide
.htaccess
index.php
license.txt

This way we get some benefits. First, by moving the system folder outside the document root we prevent possible attacks on CI core files. In this case if some vulnerability is discovered, in one of those files, the attacker will have a difficult time trying to reach for them. Second, separating the application folder from the system folder we can have as many CI applications as we need and only one CI core folder. This way if we need to upgrade CI, we only need to do it in one directory.

主站蜘蛛池模板: 肥东县| 志丹县| 镇沅| 和林格尔县| 太白县| 静海县| 柳州市| 萨迦县| 富阳市| 砀山县| 白朗县| 南投市| 东辽县| 搜索| 奈曼旗| 丹巴县| 阳泉市| 连江县| 南涧| 上蔡县| 黑山县| 凯里市| 徐州市| 天祝| 乳山市| 岐山县| 龙井市| 常州市| 上犹县| 象州县| 皮山县| 罗甸县| 丰台区| 山东| 阳城县| 页游| 黄龙县| 普定县| 京山县| 阳山县| 禹城市|