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

Putting it all together

Now that we have made all the amendments to configuration files, created the database, and created all the files necessary for the app to work (controllers, models, views, and so on) let's run through a few scenarios briefly, just to make sure we know how the app functions.

Creating a shortened URL

Let's consider an example where Lucy visits the URL Shortener app and the create controller is called by CodeIgniter, displaying the create/create.php view file. The following is the sequence of events:

  1. Lucy enters a URL in the text input and clicks on Shorten Now.
  2. Upon submitting the form, the controller validates the URL. The URL is successfully validated and the validation returns no errors.
  3. The URL entered by Lucy is then sent to the save_url()function of Urls_model that creates a unique code. The save_url() function uses the PHP construct do while and an Active Record database query to create a unique code that doesn't already exist in the database.
  4. Once a code has been created that doesn't already exist, it is saved to the database along with a MySQL timestamp.
  5. The app then displays a success message to Lucy, informing her that the URL was saved correctly. It also displays the URL for her to either click on or (more likely) copy-and-paste elsewhere.

Retrieving a URL

Let's consider an example where Jessica receives an e-mail from Lucy containing the shortened URL. The following is the sequence of events:

  1. Jessica opens the e-mail and clicks on the URL in that e-mail.
  2. Her computer opens a browser and takes her to our app. As the create controller is not the 1st uri segment, the go controller is run (we set this in the routes.php file).
  3. The go controller loads the Urls_model, passing it the url_code (that was in the first segment of uri). The fetch_url() function of Urls_model looks in the database for the code and, if found, it returns the actual web address associated with that code to the go controller.
  4. The go controller redirects the browser to the URL supplied by the model.
  5. Jessica is happy as she can look at the cute cat video Lucy sent her! Awww!
主站蜘蛛池模板: 宣化县| 昌乐县| 佳木斯市| 孙吴县| 巴彦县| 肃宁县| 大庆市| 柳州市| 黄平县| 莎车县| 金阳县| 蚌埠市| 珲春市| 凤庆县| 阳曲县| 漳浦县| 玉林市| 七台河市| 福泉市| 德庆县| 阿克陶县| 定州市| 陕西省| 丁青县| 灯塔市| 广河县| 明水县| 大竹县| 玛纳斯县| 新乡县| 从江县| 建平县| 收藏| 元谋县| 华坪县| 芦溪县| 隆尧县| 北辰区| 平阳县| 景东| 宜川县|