Showing posts with label controller. Show all posts
Showing posts with label controller. Show all posts

Wednesday, November 10, 2010

Home Page

While you can route your home page to a controller and action as we did previously, you may prefer to have a separate page set up as the leading page into your site. A good practice is to create a homepage controller and view to use as the main page to your site, this way all of the other controllers and views are tied specifically and only to the model to which they belong.

First create /app/controllers/homepages_controller.php
<?php
class HomepagesController extends AppController {

}
?>
Since the home page controller does not have an associated model, if we wanted to access any data we would define the models we want to access with the $uses variable. If the models are associated, such as our "User" model and "Carrier" model, it may not be necessary to include both, but if they are not related, such as the "Drug" model, but you want to include data from that table, you need to include both.
var $uses = array('User', 'Drug');
In our case we won't be using any data for the home page at this time so we need to create an empty array. We'll also need to create an index method.
var $uses = array();

function index() {
    $this->set('title_for_layout', 'Drug-Ed.com : Drug Education and Medication Adherence');
}
Next we need to create the home page view so create /app/views/homepages/index.ctp For now we'll leave this blank.

And the last thing we need to do is change our routing to go to the home page instead of the previously set up users page.

Edit /app/config/routes.php and replace
    Router::connect('/', array('controller'=>'users', 'action'=>'index'));
with
    Router::connect('/', array('controller'=>'homepages', 'action'=>'index'));
Now browse to http://drug-ed.com/ and verify it goes to the homepages index.

Tuesday, November 9, 2010

Page Title

From your home page observe the page title, it should be something like this: "CakePHP: the rapid development php framework: Users"

The first part of the title is defined in the default layout. The layout predefines the wrapper HTML code of every page. This file is located in /cake/libs/view/layouts/default.ctp and before editing it you should move it to /app/views/layouts/default.ctp

Next, edit the page and remove the following line:
<?php __('CakePHP: the rapid development php framework:'); ?>
Upload the file and test it and your home page title should just read "Users".

Now click on the username and the title will read "Scaffold :: View :: Users" While the default titles can be helpful, you will want to change them to reflect your site's method.

Edit /app/controllers/users_controller.php and for the first line in the index() method add the following:
$this->set("title_for_layout", 'Drug-ed.com : Drug Education and Medication Adherence');
Save the changes upload the file and test it at site root. This command will also work if you add it to /app/views/users/index.ctp, however, since you are modifying variables it is best practice to do it within the controller.

Wednesday, November 3, 2010

Controller: Reminders

Create /app/controllers/reminders_controller.php
<?php
class RemindersController extends AppController {
    var $name = 'Reminders';
    var $scaffold;
}
?>
Upload the file and browse to http://drug-ed.com/reminders to verify it works. Again the defined relationship to drugs and users should cause action links to show.

Controller: Drugs

Create /app/controllers/drugs_controller.php
<?php
class DrugsController extends AppController {
    var $name = 'Drugs';
    var $scaffold;
}
?>
Upload the file and browse to http://drug-ed.com/drugs to verify it works. Again the defined relationship to reminders should cause action links to show. Add a drug.
NameBrand NameIndicationsAdverse ReactionsCounseling PointsNotes
Diclofenac NaCataflamDiclofenac is used to relieve pain, tenderness, swelling, and stiffness caused by osteoarthritis, rheumatoid arthritis, ankylosing spondylitis, painful menstrual periods and pain from other causes.diarrhea, constipation, gas or bloating, headache, dizziness, ringing in the earsNSAIDs such as diclofenac may cause ulcers, bleeding, or holes in the stomach or intestine. People who take nonsteroidal anti-inflammatory drugs (NSAIDs) (other than aspirin) such as diclofenac may have a higher risk of having a heart attack or a stroke than people who do not take these medications.Diclofenac is in the class of Non-Steroidal Anti-Inflammatory Drugs (NSAIDs)

Controller: Users

Create /app/controllers/users_controller.php
<?php
class UsersController extends AppController {
    var $name = 'Users';
    var $scaffold;
}
?>
Upload the file and browse to http://drug-ed.com/users to verify it works. Since Users have defined relationships with Carriers and Reminders observe the action links. Click "New User" and observe the option to select the carriers entered in earlier. Create a default user for administration and testing of your site.

Controller: Carriers

Controllers provide the functionality of your application. Controllers are named plural and "controller" is explicitly listed in the name.

Create /app/controllers/carriers_controller.php
<?php
class CarriersController extends AppController {
    var $name = 'Carriers';
    var $scaffold;
}
?>
If the controller is using a model other than, or in addition to, the one with the same name as its name, you can access that model in one of two ways.
  1. Add var $uses = array('Carrier', 'Other');
  2. Access the other model directly in code via $this->Carrier->Other...
Adding the Carrier model in the Carriers controller in option 1 is unnecessary since it is automatic, but is listed only for clarity.

The variable $scaffold turns on automated scaffolding for testing and viewing of database data. After testing is complete, ensure to remove the scaffolding line to prevent unauthorized access. If you test with scaffolding, any defined methods will override the scaffolding.

Upload the file and browse to http://drug-ed.com/carriers to verify it works, then add a few carriers.
NameExtension
Verizon PCSvtext.com
AT&T PCStxt.att.net


Observe that since there is a defined relationship between Carriers and Users the actions "New User" and "List Users" are available on the Carriers page. Since there is no Users controller the links don't work yet.