- CodeIgniter Web Application Blueprints
- Rob Foster
- 365字
- 2021-08-06 19:34:30
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:
- Lucy enters a URL in the text input and clicks on Shorten Now.
- Upon submitting the form, the controller validates the URL. The URL is successfully validated and the validation returns no errors.
- The URL entered by Lucy is then sent to the
save_url()
function ofUrls_model
that creates a unique code. Thesave_url()
function uses the PHP constructdo while
and an Active Record database query to create a unique code that doesn't already exist in the database. - Once a code has been created that doesn't already exist, it is saved to the database along with a MySQL timestamp.
- 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:
- Jessica opens the e-mail and clicks on the URL in that e-mail.
- Her computer opens a browser and takes her to our app. As the
create
controller is not the 1sturi
segment, thego
controller is run (we set this in theroutes.php
file). - The
go
controller loads theUrls_model,
passing it theurl_code
(that was in the first segment ofuri
). Thefetch_url()
function ofUrls_model
looks in the database for the code and, if found, it returns the actual web address associated with that code to thego
controller. - The
go
controller redirects the browser to the URL supplied by the model. - Jessica is happy as she can look at the cute cat video Lucy sent her! Awww!
推薦閱讀
- 深度實踐OpenStack:基于Python的OpenStack組件開發(fā)
- 圖解Java數(shù)據(jù)結(jié)構(gòu)與算法(微課視頻版)
- INSTANT MinGW Starter
- 程序員考試案例梳理、真題透解與強化訓(xùn)練
- Python 3網(wǎng)絡(luò)爬蟲實戰(zhàn)
- Java Web應(yīng)用開發(fā)技術(shù)與案例教程(第2版)
- Java深入解析:透析Java本質(zhì)的36個話題
- QGIS:Becoming a GIS Power User
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實戰(zhàn)
- ExtJS高級程序設(shè)計
- Machine Learning in Java
- Django 3.0入門與實踐
- Web程序設(shè)計:ASP.NET(第2版)
- 微前端設(shè)計與實現(xiàn)
- 原型設(shè)計:打造成功產(chǎn)品的實用方法及實踐