Permalink
Browse files

Changing sample SleekMVC app to be a fully working link shortening app

  • Loading branch information...
tlhunter committed Nov 20, 2011
1 parent 4a739a4 commit 49163434a5cb5bb77c54cc1cec00eb506b60beeb
View
@@ -28,7 +28,7 @@ cache_file_directory = 'app/cache/'
database[host] = 'localhost'
database[user] = 'annarbor'
database[pass] = 'ufvNyp78N8LQdwHR'
-database[name] = 'annarbor'
+database[name] = 'sleekmvc'
; Configure your production server here (e.g. the live website)
[production_server]
View
@@ -0,0 +1,47 @@
+<?php
+namespace App;
+
+/**
+ * This controller has two actions. The first one (index) is used for showing information to
+ * the browser. A user navigates here and reads information. This action will require use of
+ * the layout. The second action is the API URL, used when a program is making a request to
+ * the app to generate a short URL. That request doesn't need the layout, so notice the
+ * auto_render_layout attribute we set to FALSE. Checkout Controller_HasLayout for more
+ * details.
+ */
+class Controller_Api extends Controller_HasLayout {
+
+ /**
+ * Browser visits /api
+ * @return void
+ */
+ public function action_index() {
+ // Here, we render the views/pages/api.php view file, pass it in the data array, and
+ // return the data as a string to stick back into the data array as the layout view's
+ // $content variable.
+ $this->page_data['content'] = \Sleek\View::render('pages/api', $this->page_data, TRUE);
+ }
+
+ /**
+ * Program visits /api/create?url=http://www.example.com
+ * @return void
+ */
+ public function action_create() {
+ // Disables the layout stuff
+ $this->auto_render_layout = FALSE;
+
+ // Get the ?url= parameter
+ $url = $this->request->get('url');
+
+ if (!$url) {
+ echo "ERROR: Empty URL";
+ } else if (!filter_var($url, FILTER_VALIDATE_URL)) {
+ echo "ERROR: Invalid URL";
+ } else {
+ $EzLink = new Model_EzLink();
+ $id = $EzLink->insertUrl($url);
+ // Controllers shouldn't really output directly, but it seems like a waste of a view file otherwise
+ echo 'http://' . $this->request->server('HTTP_HOST') . '/link/' . Model_EzLink::integerToCode($id);
+ }
+ }
+}
View
@@ -3,12 +3,13 @@
/**
* This controller is used when your app generates an error. It allows you to handle
- * various types of errors an app usually has, such as 404 and 500 errors.
+ * various types of errors an app usually has, such as 404 and 500 errors. Note that
+ * PHP error handling isn't perfect and this class won't grab all of our errors.
*/
class Controller_Error extends \Sleek\Controller_Base {
/**
- * Use this for a custom 404 error page. Note that it won't grab all 404's
+ * Use this for a custom 404 error page.
* @return void
*/
public function action_404() {
@@ -0,0 +1,53 @@
+<?php
+namespace App;
+
+/**
+ * This controller class has some useful code for displaying a page layout. You can extend
+ * this class instead of \Sleek\Controller_Base to get access to this layout code in your
+ * controllers. Make sure you call parent::preAction() and parent::postAction in your
+ * extending controller preAction and postAction methods (if they exist). Also, if some of
+ * your actions in a controller need a layout and others don't, you can set the
+ * auto_render_layout attribute to FALSE.
+ *
+ * Also, this controller has no action's of it's own, so it doesn't have any 'pages' available
+ * for the browser to hit. It is only useful for other classes to extend. For this reason I
+ * have made the class abstract.
+ */
+abstract class Controller_HasLayout extends \Sleek\Controller_Base {
+
+ /**
+ * A common array for storing view data
+ * @var array
+ */
+ protected $page_data = array();
+
+ /**
+ * Whether or not to execute the layout
+ * @var bool
+ */
+ protected $auto_render_layout = TRUE;
+
+ /**
+ * Runs before the action. Sets some default view variables.
+ * @return void
+ */
+ public function preAction() {
+ $this->page_data['title'] = 'EzLink';
+ $this->page_data['server'] = $this->request->server('HTTP_HOST');
+ }
+
+ /**
+ * Runs after the action. Executes the layout/main view file.
+ * @return void
+ */
+ public function postAction() {
+ if ($this->auto_render_layout) {
+ $EzLink = new Model_EzLink();
+ $this->page_data['count_urls'] = $EzLink->countUrls();
+ $this->page_data['count_clicks'] = $EzLink->countClicks();
+ $this->response->view('layout/main', $this->page_data);
+ }
+ }
+
+
+}
View
@@ -6,131 +6,30 @@
* @throws \Exception
*
*/
-class Controller_Home extends \Sleek\Controller_Base {
-
- /**
- * This controller shows how to cache values, and this variable represents that cache
- * @var \Sleek\Cache
- */
- protected $cache;
-
- /**
- * This function is executed once per page execution, and is run before the requested action
- * Think of it as a helpful place to put code which you want to run regardless of which action
- * a user requests. It is not required but is nice to have.
- * @return void
- */
- public function preAction() {
- $this->cache = \Sleek\Cache::getInstance();
- }
-
+class Controller_Home extends Controller_HasLayout {
/**
- * The index action is the default action of controllers (configurable in config.ini)
- * You can run this code by visiting /, /home, or /home/index
+ * This is the main page of the website.
* @return void
*/
public function action_index() {
- // The $data array is what we use for storing view data. Each index of this array
- // will be accessible in the view we execute later. You don't have to name it $data
- // if you don't want to. The below variable will be called $title in the view file.
- $data['title'] = 'Hello World!';
-
- // Use the $request object in a controller for getting data from the user
- $data['something'] = $this->request->get('something');
-
- // If we don't already have this variable in the session, make one
- if (!$this->session->random) {
- $this->session->random = rand(1, 1000);
- }
-
- // If we haven't already cached a value, go ahead and do so
- if (!$this->cache->cachedValue) {
- $this->cache->cachedValue = time();
- }
-
- // Here we set the values from above to our view array
- $data['random'] = $this->session->random;
- $data['cachedDate'] = $this->cache->cachedValue;
-
- // This is another array. We'll be using two since we want to render two view files
- $data2['title'] = 'Hallo Welt!';
-
- // Here we are rendering a view file and returning it as a string. This allows us to
- // have 'views inside views'. The view file we load will be APP_PATH/view/hello.php
- $data2['content'] = \Sleek\View::render('hello', $data, TRUE);
-
- // Here we tell the response to load a view, and tell it what data to render
- $this->response->view('layout/main', $data2);
- }
-
- /**
- * You can have as many actions in a controller as you want.
- * This page is accessible via /home/register only.
- * The third, fourth, and fifth URL parameters are passed in as arguments.
- * You'll usually want to provide default values for these arguments.
- * @param string $username
- * @param string $email
- * @param int $age
- * @return void
- */
- public function action_register($username = '', $email = '', $age = 0) {
- echo "register!";
- $people = new Model_People;
- if ($id = $people->newPerson('test')) {
- echo "New person was created! Their id is $id.";
- } else {
- echo "Error creating person. Error is {$people->lastErrorPassThru()}.";
- }
- }
-
- /**
- * This page is loaded when you visit /home/people
- * @return void
- */
- public function action_people() {
- $people = new Model_People;
- $peopleList = $people->getAllPeopleSimple();
- $data['people'] = $peopleList;
- $this->response->view('people', $data);
+ $this->page_data['content'] = \Sleek\View::render('pages/home', $this->page_data, TRUE);
}
/**
- * This action is executed when you visit /home/emailtest
- * It generates and sends an email.
+ * This is the page the user hits when submitting a URL via their browser.
* @return void
*/
- public function action_emailtest() {
- $email = new \Sleek\Email;
- $email->setRecipient('user@example.com')
- ->setSubject('This is a test email from Sleek')
- ->setTypeHtml()
- ->setBody('<em>Hey there</em>, how is it <strong>going</strong>?')
- ->setSender('user@example.com')
- ->debug()
- ->send();
- }
+ public function action_submit() {
+ $url = $this->request->post('url');
+ $this->page_data['code'] = FALSE;
- /**
- * This function is executed once per page load after the requested action has run
- * It is not needed, but can be helpful
- * @return void
- */
- public function postAction() {
+ if ($url && filter_var($url, FILTER_VALIDATE_URL)) {
+ $EzLink = new Model_EzLink();
+ $id = $EzLink->insertUrl($url);
+ $this->page_data['code'] = Model_EzLink::integerToCode($id);
+ }
+ $this->page_data['content'] = \Sleek\View::render('pages/submit', $this->page_data, TRUE);
}
- /**
- * This function is like a per-controller 404 action. But, don't think of it as
- * being useful for just catching 404's. You can use it for cool things like a
- * short URL service. Think example.com/product/a7fc3
- * @param string $argument1
- * @param string $argument2
- * @param string $argument3
- * @return void
- */
- public function noAction($argument1 = '', $argument2 = '', $argument3 = '') {
- $requestedAction = $this->request->urlAction();
- echo "You requested the $requestedAction action of the Home controller, which doesn't exist.";
- }
-
}
View
@@ -0,0 +1,24 @@
+<?php
+namespace App;
+
+class Controller_Link extends \Sleek\Controller_Base {
+
+ /**
+ * This function is run when someone visits /link/abcDEF123
+ * @return void
+ */
+ public function noAction() {
+ $id = Model_EzLink::codeToInteger($this->request->urlAction());
+ $hidden = $this->request->get('h') !== NULL;
+
+ $EzLink = new Model_EzLink();
+ $url = $EzLink->getUrlById($id);
+ $EzLink->clickUrl($id, date("Y"), date("n"));
+
+ if ($hidden) {
+ echo "<meta http-equiv=\"refresh\" content=\"0; URL=$url\">";
+ } else {
+ $this->response->redirect($url);
+ }
+ }
+}
@@ -0,0 +1,25 @@
+<?php
+namespace App;
+
+class Controller_Statistics extends Controller_HasLayout {
+ /**
+ * This is the page when the user visits /statistics
+ * @return void
+ */
+ public function action_index() {
+ $this->page_data['content'] = \Sleek\View::render('pages/statistics', $this->page_data, TRUE);
+ }
+
+ /**
+ * This is an interesting method. It returns an image, not HTML!
+ * @return void
+ */
+ public function action_image() {
+ $this->auto_render_layout = FALSE;
+ $EzLink = new Model_EzLink();
+ $image = $EzLink->getStatisticsImage();
+
+ $this->response->header('Content-type', 'image/png');
+ imagepng($image);
+ }
+}
View
Binary file not shown.
Oops, something went wrong.

0 comments on commit 4916343

Please sign in to comment.