Skip to content
Browse files

Now allows "global" variables to be created in the controller.

  • Loading branch information...
1 parent bf4ec0c commit f5b2d789bfb77349212ef6f68794c792ccfedb6a @BaylorRae committed Mar 14, 2011
Showing with 16 additions and 8 deletions.
  1. +1 −0 app/controllers/app_controller.php
  2. +1 −1 app/views/layouts/application.html.php
  3. +8 −3 libraries/object.php
  4. +6 −4 libraries/router.php
View
1 app/controllers/app_controller.php
@@ -1,5 +1,6 @@
<?php
class AppController extends Controller {
+ public $site_title = 'PHP MVC Framework';
}
View
2 app/views/layouts/application.html.php
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
-<title>HTML5 Document</title>
+<title><?php echo $site_title ?></title>
<meta charset="utf-8" />
</head>
View
11 libraries/object.php
@@ -3,14 +3,19 @@
// available to all extending classes
class Object {
- public static $user_vars = array();
+ public static $__user_vars = array();
public function __set($name, $value) {
- self::$user_vars[$name] = $value;
+ self::$__user_vars[$name] = $value;
}
public function __get($name) {
- return (isset(self::$user_vars[$name])) ? self::$user_vars[$name] : null;
+ return (isset(self::$__user_vars[$name])) ? self::$__user_vars[$name] : null;
}
+ public function get_user_vars($class) {
+ $vars = get_object_vars($class);
+
+ self::$__user_vars = array_merge($vars, self::$__user_vars);
+ }
}
View
10 libraries/router.php
@@ -71,15 +71,17 @@ public static function dispatch($format) {
if( class_exists($class_name) ) {
$tmp_class = new $class_name();
-
+
// run the matching action
if( is_callable(array($tmp_class, $action)) ) {
$tmp_class->$action();
}else
die('The action <strong>' . $action . '</strong> could not be called from the controller <strong>' . $class_name . '</strong>');
}else
die('The class <strong>' . $class_name . '</strong> could not be found in <pre>' . APP_PATH . 'controllers/' . $controller . '_controller.php</pre>');
-
+
+ self::get_user_vars($tmp_class);
+
// include the view file
// self::load_view($controller, $action, $format);
@@ -120,7 +122,7 @@ public static function load_view($controller, $action, $format) {
if( !empty($view_path) ) {
unset($controller, $action, $format);
- foreach( self::$user_vars as $var => $value ) {
+ foreach( self::$__user_vars as $var => $value ) {
$$var = $value;
}
@@ -164,7 +166,7 @@ public static function view_path($controller, $action, $format) {
}
public static function load_layout($filename) {
- foreach( self::$user_vars as $var => $value ) {
+ foreach( self::$__user_vars as $var => $value ) {
$$var = $value;
}

4 comments on commit f5b2d78

@titu2012

Hi Baylore,
it's a very nice tutorial on MVC you have uploaded to youtube. and am not sure it's a right way to put's my comments on here or not. Am new in php MVC framework. And i like ur way of putting/coding, which you have shown so far in those part1,2,3,4,5 and 6. However, i was wondering if you could have done another part which would connect to a DB and have some functions on DDL,DML. or could direct me somewhere i could find something similar to your mvc framework style. Many thnx for your effort and time.

@BaylorRae
Owner

@titu2012 I think your best bet for DB interaction would be http://www.phpactiverecord.org/. It's easy to implement and provides an awesome syntax when in an MVC environment.

@titu2012

Hi Baylor,
Thnx for ur link. however, am facing one issue, whereby i can't access edit/add through url like http://localhost/mvc/welcome/add. And i tried in submit form like this "form id="login" action="welcome" method="post">" but it shows "The requested URL /starkb/welcome was not found on this server." and if i leave submit form with action empty like this form id="login" action=" " method="post"> it shows error msgs:

The file _controller.php could not be found at
C:\wamp\www\mvc/app/controllers/_controller.php

Any advice, am using ur source code. Appreciate your advice. Thanks again

@titu2012

And another inquire is how do i load any model in ur mvc framework? should i do like add model.php in libraries, then in models > i add like app_model.php extends Model and then login_model.php extends AppModel ? if so, how could i access a method defined in model.php cause i tried to access it am getting an error like this : Fatal error: Call to undefined function logcheck() in C:\wamp\www\mvc\app\controllers\login_controller.php on line 22" even though i already included model.php in core.php file.

Would be able to help me on this? am sorry, am new in this environment and may be am doing something wrong.
Your assistance would be highly appreciated.

Please sign in to comment.
Something went wrong with that request. Please try again.