| | 121 | If your application has errors or if you want to print something to the log, then |
| | 122 | any where in your code if you use the built in php function "error_log('some message')" |
| | 123 | it will write the message to the trax/log/development.log or whatever mode your in log file. |
| | 124 | |
| | 125 | == Description of contents |
| | 126 | |
| | 127 | trax/app |
| | 128 | Holds all the code that's specific to this particular application. |
| | 129 | |
| | 130 | trax/app/controllers |
| | 131 | Holds controllers that should be named like weblog_controller.php for |
| | 132 | automated URL mapping. All controllers should descend from |
| | 133 | ActionController. |
| | 134 | |
| | 135 | trax/app/libs |
| | 136 | Application specific libraries. Basically, any kind of custom code that doesn't |
| | 137 | belong controllers, models, or helpers. This directory is in the includes path. |
| | 138 | |
| | 139 | trax/app/models |
| | 140 | Holds models that should be named like post.rb. |
| | 141 | Most models will descent from ActiveRecord. |
| | 142 | |
| | 143 | trax/app/views |
| | 144 | Holds the template files for the view that should be named like |
| | 145 | weblog/index.phtml for the WeblogController->index action. |
| | 146 | |
| | 147 | trax/app/helpers |
| | 148 | Holds view helpers that should be named like weblog_helper.php. |
| | 149 | |
| | 150 | trax/config |
| | 151 | Configuration files for the Trax environment, the routing map, the database, and other dependencies. |
| | 152 | |
| | 153 | trax/lib |
| | 154 | Trax libraries. This directory is in the includes path. |
| | 155 | |
| | 156 | trax/script |
| | 157 | Helper scripts for automation and generation. |
| | 158 | |
| | 159 | public / public_html |
| | 160 | The directory available for the web server. Contains sub-directories for images, stylesheets, |
| | 161 | and javascripts. Also contains the .htaccess, dispatcher, and default HTML files. |
| | 162 | |
| 137 | | |
| 138 | | == Description of contents |
| 139 | | |
| 140 | | trax/app |
| 141 | | Holds all the code that's specific to this particular application. |
| 142 | | |
| 143 | | trax/app/controllers |
| 144 | | Holds controllers that should be named like weblog_controller.php for |
| 145 | | automated URL mapping. All controllers should descend from |
| 146 | | ActionController. |
| 147 | | |
| 148 | | trax/app/libs |
| 149 | | Application specific libraries. Basically, any kind of custom code that doesn't |
| 150 | | belong controllers, models, or helpers. This directory is in the includes path. |
| 151 | | |
| 152 | | trax/app/models |
| 153 | | Holds models that should be named like post.rb. |
| 154 | | Most models will descent from ActiveRecord. |
| 155 | | |
| 156 | | trax/app/views |
| 157 | | Holds the template files for the view that should be named like |
| 158 | | weblog/index.phtml for the WeblogController->index action. |
| 159 | | |
| 160 | | trax/app/helpers |
| 161 | | Holds view helpers that should be named like weblog_helper.php. |
| 162 | | |
| 163 | | trax/config |
| 164 | | Configuration files for the Trax environment, the routing map, the database, and other dependencies. |
| 165 | | |
| 166 | | trax/lib |
| 167 | | Trax libraries. This directory is in the includes path. |
| 168 | | |
| 169 | | trax/script |
| 170 | | Helper scripts for automation and generation. |
| 171 | | |
| 172 | | public / public_html |
| 173 | | The directory available for the web server. Contains sub-directories for images, stylesheets, |
| 174 | | and javascripts. Also contains the .htaccess, dispatcher, and default HTML files. |
| 175 | | |
| | 187 | |
| | 188 | |
| | 189 | == Example App |
| | 190 | |
| | 191 | URL: |
| | 192 | http://traxtest.mytechsupport.com |
| | 193 | |
| | 194 | Trax Code: |
| | 195 | |
| | 196 | ### config/database.ini ### |
| | 197 | [development] |
| | 198 | phptype = mysql |
| | 199 | database = mydb_development |
| | 200 | host = localhost |
| | 201 | username = user |
| | 202 | password = ****** |
| | 203 | persistent = 1 |
| | 204 | |
| | 205 | ### MySQL Table: ### |
| | 206 | CREATE TABLE `files` ( |
| | 207 | `id` int(11) NOT NULL auto_increment, |
| | 208 | `owner` varchar(50) NOT NULL default '', |
| | 209 | `name` varchar(50) NOT NULL default '', |
| | 210 | `contents` text NOT NULL, |
| | 211 | `created_at` datetime NOT NULL default '0000-00-00 00:00:00', |
| | 212 | PRIMARY KEY (`id`) |
| | 213 | ); |
| | 214 | |
| | 215 | ### config/routes.php ### |
| | 216 | // this tells trax to route the root domain to the file_list controller |
| | 217 | // same as just going to http://traxtest.mytechsupport.com/file_list |
| | 218 | $router->connect( "", array(":controller" => "file_list") ); |
| | 219 | |
| | 220 | ### app/controllers/file_list_controller.php #### |
| | 221 | class FileListController extends ApplicationController { |
| | 222 | function index() { |
| | 223 | $file = new File(); |
| | 224 | $this->files = $file->find_all(); |
| | 225 | } |
| | 226 | |
| | 227 | function edit() { |
| | 228 | $file = new File(); |
| | 229 | $this->file = $file->find($_REQUEST['id']); |
| | 230 | if($_POST) { |
| | 231 | if(!$this->file->save($_REQUEST['file'])) { |
| | 232 | $_SESSION['flash']['error'] = @implode("<br>",$this->file->errors); |
| | 233 | } else { |
| | 234 | $_SESSION['flash']['notice'] = "File saved successfully."; |
| | 235 | $this->redirect_to = "/file_list"; |
| | 236 | } |
| | 237 | } |
| | 238 | } |
| | 239 | |
| | 240 | function add() { |
| | 241 | $this->file = new File($_REQUEST['file']); |
| | 242 | if($_POST) { |
| | 243 | if(!$this->file->save()) { |
| | 244 | $_SESSION['flash']['error'] = @implode("<br>",$this->file->errors); |
| | 245 | } else { |
| | 246 | $_SESSION['flash']['notice'] = "File added successfully."; |
| | 247 | $this->redirect_to = "/file_list"; |
| | 248 | } |
| | 249 | } |
| | 250 | } |
| | 251 | |
| | 252 | function delete() { |
| | 253 | $file = new File(); |
| | 254 | $this->file = $file->find($_REQUEST['id']); |
| | 255 | if(is_object($this->file)) { |
| | 256 | if(!$this->file->delete()) { |
| | 257 | $_SESSION['flash']['error'] = "Error deleting file."; |
| | 258 | $this->render_action = "edit"; |
| | 259 | } else { |
| | 260 | $_SESSION['flash']['notice'] = "File deleted successfully."; |
| | 261 | $this->redirect_to = "/file_list"; |
| | 262 | } |
| | 263 | } else { |
| | 264 | $_SESSION['flash']['error'] = "Error can't find file to delete."; |
| | 265 | $this->render_action = "edit"; |
| | 266 | } |
| | 267 | } |
| | 268 | } |
| | 269 | |
| | 270 | Model |
| | 271 | ### app/models/file.php #### |
| | 272 | class File extends ActiveRecord { |
| | 273 | |
| | 274 | function validate_name() { |
| | 275 | return array(!empty($this->name), 'Please don\'t leave name blank.'); |
| | 276 | } |
| | 277 | |
| | 278 | function validate_owner() { |
| | 279 | return array(!empty($this->owner), 'Please don\'t leave owner blank.'); |
| | 280 | } |
| | 281 | |
| | 282 | function validate_contents() { |
| | 283 | if(strlen($this->contents) < 15) |
| | 284 | return array(false, 'Contents needs to be at least 15 characters long.'); |
| | 285 | else |
| | 286 | return array(true, null); |
| | 287 | } |
| | 288 | |
| | 289 | } |
| | 290 | |
| | 291 | Views |
| | 292 | ### app/views/file_list/add.phtml #### |
| | 293 | <h1>FileList->add</h1> |
| | 294 | <form method="post" action="/file_list/add"> |
| | 295 | <? $this->render_partial("form") ?> |
| | 296 | </form> |
| | 297 | <br> |
| | 298 | <a href="/file_list">back</a> |
| | 299 | |
| | 300 | ### app/views/file_list/edit.phtml #### |
| | 301 | <h1>FileList->edit</h1> |
| | 302 | <form method="post" action="/file_list/edit/<?=$file->id?>"> |
| | 303 | <? $this->render_partial("form") ?> |
| | 304 | </form> |
| | 305 | <br> |
| | 306 | <a href="/file_list">back</a> <a href="/file_list/delete/<?=$file->id?>">delete</a> |
| | 307 | |
| | 308 | Partial Views |
| | 309 | ### app/views/file_list/_form.phtml #### |
| | 310 | <table> |
| | 311 | <tr> |
| | 312 | <td>Id: </td> |
| | 313 | <td><?=$file->id?></td> |
| | 314 | </tr> |
| | 315 | <tr> |
| | 316 | <td>Name: </td> |
| | 317 | <td><input type="text" name="file[name]" value="<?=$file->name?>"/></td> |
| | 318 | </tr> |
| | 319 | <tr> |
| | 320 | <td>Owner: </td> |
| | 321 | <td><input type="text" name="file[owner]" value="<?=$file->owner?>"/></td> |
| | 322 | <tr> |
| | 323 | <td valign="top">Contents: </td> |
| | 324 | <td><textarea name="file[contents]" rows="10" cols="80"><?=$file->contents?></textarea></td> |
| | 325 | </tr> |
| | 326 | <tr> |
| | 327 | <td>Created: </td> |
| | 328 | <td><?=$file->created_at?></td> |
| | 329 | </tr> |
| | 330 | <tr> |
| | 331 | <td colspan="2"><input type="submit" value=" Save "/></td> |
| | 332 | </tr> |
| | 333 | </table> |
| | 334 | |
| | 335 | Site layout file: |
| | 336 | ### app/layouts/public.phtml |
| | 337 | <html> |
| | 338 | <head> |
| | 339 | <title>PHP on Trax</title> |
| | 340 | </head> |
| | 341 | <body> |
| | 342 | |
| | 343 | <? if($_SESSION['flash']['notice']): ?> |
| | 344 | <font color="green"><?=$_SESSION['flash']['notice']?></font><br><br> |
| | 345 | <? elseif($_SESSION['flash']['error']): ?> |
| | 346 | <font color="red"><?=$_SESSION['flash']['error']?></font><br><br> |
| | 347 | <? endif; ?> |
| | 348 | |
| | 349 | <?=$content_for_layout?> |
| | 350 | |
| | 351 | </body> |
| | 352 | </html> |
| | 353 | |
| | 354 | |