· 6 years ago · Jan 04, 2020, 12:58 PM
1CodeIgniter Web Application Blueprints.pdf
2
3
4application/
5├── controllers/
6│ ├── create.php
7│ ├── go.php
8├── models/
9│ ├── urls_model.php
10├── views/create/
11│ ├── create.php
12├── views/nav/
13│ ├── top_nav.php
14├── language/english/
15├── en_admin_lang.php
16
17
181)Create database & table
19
20CREATE DATABASE `urls`;
21
22USE `urls`;
23
24CREATE TABLE `urls` (
25`url_id` int(11) NOT NULL AUTO_INCREMENT,
26`url_code` varchar(10) NOT NULL,
27`url_address` text NOT NULL,
28`url_created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
29PRIMARY KEY (`url_id`)
30) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
31
32
332)Add data in route.php
34
35$route['default_controller'] = "create";
36
37
38$route['create'] = "create/index";
39$route['(:any)'] = "go/index";
40
41
423)models/urls_model.php
43
44<?php if ( ! defined('BASEPATH')) exit('No direct script access
45allowed');
46class Urls_model extends CI_Model {
47function __construct() {
48parent::__construct();
49}
50function save_url($data) {
51/*
52Let's see if the unique code already exists in
53the database. If it does exist then make a new
54one and we'll check if that exists too.
55Keep making new ones until it's unique.
56When we make one that's unique, use it for our url
57*/
58do {
59$url_code = random_string('alnum', 8);
60$this->db->where('url_code = ', $url_code);
61$this->db->from('urls');
62$num = $this->db->count_all_results();
63} while ($num >= 1);
64$query = "INSERT INTO `urls` (`url_code`, `url_address`) VALUES (?,?) ";
65$result = $this->db->query($query, array($url_code, $data['url_address']));
66if ($result) {
67return $url_code;
68} else {
69return false;
70}
71}
72function fetch_url($url_code) {
73$query = "SELECT * FROM `urls` WHERE `url_code` = ? ";
74$result = $this->db->query($query, array($url_code));
75if ($result) {
76return $result;
77} else {
78return false;
79}
80}
81}
82
834)Create Views Create.php
84/views/create/create.php
85<div class="page-header">
86<h1><?php echo $this->lang->line('system_system_name'); ?></
87h1>
88</div>
89<p><?php echo $this->lang->line('encode_instruction_1'); ?></p>
90<?php if (validation_errors()) : ?>
91<?php echo validation_errors(); ?>
92<?php endif ; ?>
93<?php if ($success_fail == 'success') : ?>
94<div class="alert alert-success">
95<strong><?php echo $this->lang->line('common_form_elements_
96success_notifty'); ?></strong> <?php echo $this->lang->line('encode_
97encode_now_success'); ?>
98</div>
99<?php endif ; ?>
100<?php if ($success_fail == 'fail') : ?>
101<div class="alert alert-danger">
102<strong><?php echo $this->lang->line('common_form_elements_
103error_notifty'); ?> </strong> <?php echo $this->lang->line('encode_
104encode_now_error'); ?>
105</div>
106<?php endif ; ?>
107<?php echo form_open('create') ; ?>
108<div class="row">
109<div class="col-lg-12">
110<div class="input-group">
111<input type="text" class="form-control" name="url_
112address" placeholder="<?php echo $this->lang->line('encode_type_url_
113here'); ?>">
114<span class="input-group-btn">
115<button class="btn btn-default" type="submit"><?php
116echo $this->lang->line('encode_encode_now'); ?></button>
117</span>
118</div><!-- /input-group -->
119</div><!-- /.col-lg-6 -->
120</div><!-- /.row -->
121<?php echo form_close() ; ?>
122<br />
123<?php if ($encoded_url == true) : ?>
124<div class="alert alert-info">
125<strong><?php echo $this->lang->line('encode_encoded_url');
126?> </strong>
127<?php echo anchor($encoded_url, $encoded_url) ; ?>
128</div>
129<?php endif ; ?>
130
1315)Create View-nav/top_nav.php
132
133<!-- Fixed navbar -->
134<div class="navbarnavbar-inverse navbar-fixed-top" role="navigation">
135<div class="container">
136<div class="navbar-header">
137<button type="button" class="navbar-toggle" data-toggle="collapse"
138data-target=".navbar-collapse">
139<span class="sr-only">Toggle navigation</span>
140<span class="icon-bar"></span>
141<span class="icon-bar"></span>
142<span class="icon-bar"></span>
143</button>
144<a class="navbar-brand" href="#"><?php echo $this->lang->line('system_
145system_name'); ?></a>
146</div>
147<div class="navbar-collapse collapse">
148<ul class="navnavbar-nav">
149<li class="active"><?php echo anchor('create', 'Create') ; ?></li>
150</ul>
151</div><!--/.navbar-collapse -->
152</div>
153</div>
154<div class="container theme-showcase" role="main">
155
156
1576)Create Controller-create.php
158
159<?php if (!defined('BASEPATH')) exit('No direct script access
160allowed');
161class Create extends MY_Controller {
162function __construct() {
163parent::__construct();
164$this->load->helper('string');
165$this->load->library('form_validation');
166$this->form_validation->set_error_delimiters('<div
167class="alert alert-danger">', '</div>');
168}
169public function index() {
170$this->form_validation->set_rules('url_address', $this->
171lang->line('create_url_address'),
172'required|min_length[1]|max_length[1000]|trim');
173if ($this->form_validation->run() == FALSE) {
174// Set initial values for the view
175$page_data = array('success_fail' => null,
176'encoded_url' => false);
177$this->load->view('common/header');
178$this->load->view('nav/top_nav');
179$this->load->view('create/create', $page_data);
180$this->load->view('common/footer');
181} else {
182// Begin to build data to be passed to database
183$data = array(
184'url_address' => $this->input->post('url_address'),
185);
186$this->load->model('Urls_model');
187if ($res = $this->Urls_model->save_url($data)) {
188$page_data['success_fail'] = 'success';
189$page_data['encoded_url'] = $res;
190} else {
191// Some sort of error, set to display error
192message
193$page_data['success_fail'] = 'fail';
194}
195// Build link which will be displayed to the user
196$page_data['encoded_url'] = base_url() . '/' . $res;
197$this->load->view('common/header');
198$this->load->view('nav/top_nav');
199$this->load->view('create/create', $page_data);
200$this->load->view('common/footer');
201}
202}
203}
204
2057)Create controllers/go.php
206
207<?php if (!defined('BASEPATH')) exit('No direct script access
208allowed');
209class Go extends MY_Controller {
210function __construct() {
211parent::__construct();
212$this->load->helper('string');
213}
214public function index() {
215if (!$this->uri->segment(1)) {
216redirect (base_url());
217} else {
218$url_code = $this->uri->segment(1);
219$this->load->model('Urls_model');
220$query = $this->Urls_model->fetch_url($url_code);
221if ($query->num_rows() == 1) {
222foreach ($query->result() as $row) {
223$url_address = $row->url_address;
224}
225redirect (prep_url($url_address));
226} else {
227$page_data = array('success_fail' => null,
228'encoded_url' => false);
229$this->load->view('common/header');
230$this->load->view('nav/top_nav');
231$this->load->view('create/create', $page_data);
232$this->load->view('common/footer');
233}
234}
235}
236}