├── public ├── js │ └── app.js ├── css │ ├── admin │ │ └── style.css │ └── style.css ├── img │ └── ap.jpg ├── .htaccess └── index.php ├── log └── .gitignore ├── app ├── modules │ └── Admin │ │ ├── views │ │ ├── pages │ │ │ └── index.phtml │ │ ├── posts │ │ │ └── index.phtml │ │ ├── index │ │ │ └── index.phtml │ │ └── layouts │ │ │ └── admin.phtml │ │ ├── controllers │ │ ├── Pages.php │ │ ├── Posts.php │ │ └── Index.php │ │ ├── _init.php │ │ └── config │ │ └── routes.ini ├── views │ ├── restfull │ │ ├── form.phtml │ │ ├── edit.phtml │ │ ├── new.phtml │ │ ├── show.phtml │ │ └── index.phtml │ ├── index │ │ └── index.phtml │ ├── backend │ │ ├── posts │ │ │ └── index.phtml │ │ └── index │ │ │ └── index.phtml │ ├── layouts │ │ ├── frontend.phtml │ │ └── backend.phtml │ ├── application │ │ └── notfound.phtml │ └── error │ │ └── error.phtml ├── controllers │ ├── Admin.php │ ├── Backend │ │ ├── Backend.php │ │ ├── Index.php │ │ └── Posts.php │ ├── Index.php │ ├── Error.php │ ├── Application.php │ └── Restfull.php ├── models │ └── Post.php ├── plugins │ ├── Log.php │ └── AuthToken.php └── Bootstrap.php ├── vendor └── .gitignore ├── .gitignore ├── config ├── routing.ini └── application.ini.default ├── lib ├── Lycan │ └── Validations │ │ ├── Validators │ │ ├── Exclusion.php │ │ ├── Inclusion.php │ │ ├── Presence.php │ │ ├── Clusivity.php │ │ ├── Confirmation.php │ │ ├── Email.php │ │ ├── Format.php │ │ ├── Each.php │ │ ├── Acceptance.php │ │ ├── Length.php │ │ └── Numericality.php │ │ ├── Validator.php │ │ ├── ValidatableInterface.php │ │ ├── Errors.php │ │ └── Validate.php ├── eYaf │ ├── Request.php │ ├── Logger.php │ └── Layout.php └── Helper │ └── Html.php └── README.md /public/js/app.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/css/admin/style.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /log/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /app/modules/Admin/views/pages/index.phtml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /app/views/restfull/form.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/views/restfull/edit.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/views/restfull/new.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/views/restfull/show.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /public/css/style.css: -------------------------------------------------------------------------------- 1 | footer { 2 | font-size: 70%;} 3 | -------------------------------------------------------------------------------- /app/views/restfull/index.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/modules/Admin/views/posts/index.phtml: -------------------------------------------------------------------------------- 1 |
Posts Index
3 | -------------------------------------------------------------------------------- /public/img/ap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akDeveloper/yaf_base_application/HEAD/public/img/ap.jpg -------------------------------------------------------------------------------- /public/.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteCond %{REQUEST_FILENAME} !-f 3 | RewriteRule .* index.php 4 | -------------------------------------------------------------------------------- /app/views/backend/index/index.phtml: -------------------------------------------------------------------------------- 1 | title = "Hello Backend!" ?> 2 |= $this->title ?>
3 | -------------------------------------------------------------------------------- /app/controllers/Admin.php: -------------------------------------------------------------------------------- 1 | heading = 'Posts'; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /app/modules/Admin/_init.php: -------------------------------------------------------------------------------- 1 | getRouter()->addConfig($routes->admin); 7 | -------------------------------------------------------------------------------- /app/controllers/Index.php: -------------------------------------------------------------------------------- 1 | heading = 'Home Page'; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /app/modules/Admin/config/routes.ini: -------------------------------------------------------------------------------- 1 | ;Admin routes 2 | admin.admin_index.type="rewrite" 3 | admin.admin_index.match="/(admin|admin/)$" 4 | admin.admin_index.route.module="admin" 5 | admin.admin_index.route.controller="index" 6 | admin.admin_index.route.action="index" 7 | -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | bootstrap() //call bootstrap methods defined in Bootstrap.php 7 | ->run(); 8 | ?> 9 | -------------------------------------------------------------------------------- /app/modules/Admin/views/layouts/admin.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |: in file getFile()?> at line getLine()?>34 | 35 |
getTraceAsString()?>
38 |
40 |
41 |
42 | 45 | Parameters: 46 |
47 | 48 |49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Errors.php: -------------------------------------------------------------------------------- 1 | getArrayCopy(); 17 | 18 | foreach ($array as $k=>$v) $this->offsetUnset($k); 19 | } 20 | 21 | public function isEmpty() 22 | { 23 | return $this->count() == 0; 24 | } 25 | 26 | public function contains($attribute) 27 | { 28 | return $this->offsetExists($attribute); 29 | } 30 | 31 | public function add($attribute, $message=null, $options=array()) 32 | { 33 | $message = $this->_normalize_message($attribute, $message, $options); 34 | !$this->offsetExists($attribute) ? $this->offsetSet($attribute, array()) : null; 35 | $val = $this->offsetGet($attribute); 36 | array_push($val, $message); 37 | $this->offsetSet($attribute, $val); 38 | } 39 | 40 | private function _normalize_message($attribute, $message, $options) 41 | { 42 | $message = $message ?: ':invalid'; 43 | 44 | if ( 0 === strpos($message, ':') ) { 45 | return $this->generateMessage($attribute, $message, $options); 46 | } elseif (is_callable($message)) { 47 | return $message(); 48 | } else { 49 | return $message; 50 | } 51 | } 52 | 53 | public function generateMessage($attribute, $type=':invalid', $options=array()) 54 | { 55 | $message = null; 56 | $type = substr($type, 1); 57 | 58 | if (isset($options['message'])) { 59 | if (0 === strpos($options['message'], ':')) 60 | $type = substr($options['message'], 1); 61 | else 62 | $message = $options['message']; 63 | } 64 | 65 | return $message ?: $type; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A base application for Yaf framework 2 | 3 | Yaf framework documentation can be found at [php.net](http://www.php.net/manual/en/book.yaf.php) 4 | 5 | ## Requirements 6 | 7 | * Yaf php extension. Download and install from [Pecl](http://pecl.php.net/package/yaf) 8 | * PHP 5.3+ 9 | * Mysql server 10 | * Apache, Nginx or Lighttpd web server. 11 | * mod_rewrite and .htaccess enabled for Apache web server. 12 | 13 | ## Configuration 14 | 15 | * Info about setting up a server for Yaf can be found [here](http://www.php.net/manual/en/yaf.examples.php) 16 | * Rename `config/application.ini.default` to `config/application.ini` 17 | * If you have PHP 5.4 you can use the internal web server to test the project. 18 | * `cd yaf_base_application/public` 19 | * `php -S localhost:8000` 20 | * This project uses PHP 5.3 namespaces so `yaf.use_namespace` should be turned on. 21 | 22 | ## Additions 23 | 24 | * ~~A simple ORM database layer `lib/Orm`. Yaf Models extend `lib/Orm/Entity` class. (More documentation soon at wiki)~~ 25 | * Validation library `lib/Validations` from [another project](https://github.com/akDeveloper/Lycan) of mine for validating classes. 26 | * A Layout class that allows to render views inside a base html layout `lib/Layout.php`. Layouts directory can be defined in application.ini 27 | * A Logger class `lib/Logger.php` and a `LoggerPlugin` to log info about requests and database queries. (Make sure that log directory is readable.) 28 | * A custom Request class `lib/Request.php` that extends `Yaf\Request\Http` and offers input filter for request params, posts and queries. 29 | * ~~A Paginator `lib/Paginator` forked from Laravel framework and adjust it to work with `lib/Orm`~~ 30 | * An Authenticity token plugin `AuthTokenPlugin`to prevent Cross-site request forgery (csrf). Can be turned on/off from application.ini 31 | * A base `ApplicationController` which adds some base functionality like 404 not found page. 32 | * A `RestfullController` to make easy crud (create, read, update, delete) actions. 33 | * An `ErrorController` to catch all exceptions and display a page with error info and bugtrace. 34 | * Custom error_handler to catch errors and throws Exceptions. 35 | * Custom _init.php file for modules for extra configuration. 36 | * Some base helper classes `lib/Helper` 37 | -------------------------------------------------------------------------------- /app/plugins/AuthToken.php: -------------------------------------------------------------------------------- 1 | 10 | * application.protect_from_csrf=1 11 | * 12 | * 13 | * Then you must define an input hidden field in each html form you submit. 14 | *
15 | *
16 | *
17 | *
18 | * After submission of the form, the plugin will attempt to validate the
19 | * auth_token an will throw an \Exception if tokens are not equal.
20 | */
21 | class AuthTokenPlugin extends Yaf\Plugin_Abstract
22 | {
23 |
24 | public function routerShutdown(Yaf\Request_Abstract $request ,
25 | Yaf\Response_Abstract $response
26 | ) {
27 | $this->auth_token();
28 | }
29 |
30 | public function dispatchLoopStartup(Yaf\Request_Abstract $request,
31 | Yaf\Response_Abstract $response
32 | ){
33 |
34 | $this->verify_auth_token($request);
35 | }
36 |
37 | protected function verify_auth_token($request)
38 | {
39 | $config = Yaf\Application::app()->getConfig();
40 |
41 | if ( $config['application']['protect_from_csrf']
42 | && $request->isPost()
43 | ) {
44 |
45 | $post = $request->getPost();
46 |
47 | if ( !isset($post['_auth_token'])
48 | || $post['_auth_token'] !== $this->auth_token()
49 | ){
50 | throw new \Exception('Invalid authenticity token!');
51 | } else {
52 | $session = Yaf\Session::getInstance();
53 | $session->auth_token = NULL;
54 | $this->auth_token();
55 | }
56 | }
57 | }
58 |
59 | /**
60 | * Creates a random token, ancodes it with Base64 and stores it to session
61 | *
62 | * @return string The authenticity token string.
63 | */
64 | protected function auth_token()
65 | {
66 | $session = Yaf\Session::getInstance();
67 | $session->auth_token = $session->auth_token
68 | ?: base64_encode(sha1(uniqid(rand(), true)));
69 | return $session->auth_token;
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/lib/Lycan/Validations/Validators/Each.php:
--------------------------------------------------------------------------------
1 | attributes = isset($options['attributes'])
17 | ? (
18 | !is_array($options['attributes'])
19 | ? array($options['attributes'])
20 | : $options['attributes']
21 | )
22 | : array();
23 |
24 | if (empty($this->attributes)) {
25 |
26 | throw new \Exception('attributes cannot be empty');
27 | }
28 |
29 | if (isset($options['attributes'])) {
30 | unset($options['attributes']);
31 | }
32 |
33 | parent::__construct($options);
34 |
35 | $this->check_validity();
36 | }
37 |
38 | public function validate($record)
39 | {
40 | $if = $this->validates_if($record);
41 | if (false == $if) {
42 |
43 | return true;
44 | }
45 |
46 | foreach ($this->attributes as $attribute) {
47 |
48 | $value = $record->readAttributeForValidation($attribute);
49 |
50 | if ( (null === $value && isset($this->options['allow_null'])
51 | && true == $this->options['allow_null'])
52 | || (empty($value) && isset($this->options['allow_empty'])
53 | && true == $this->options['allow_empty'])
54 | ) {
55 | continue;
56 | }
57 |
58 | $this->validateEach($record, $attribute, $value);
59 | }
60 | }
61 |
62 | protected function validates_if($record)
63 | {
64 | if (isset($this->options['if'])) {
65 | $if = $this->options['if'];
66 | if (is_callable($if)) {
67 | return $if($record);
68 | } else if (method_exists($record, $if)) {
69 | return $record->$if();
70 | }
71 | }
72 | return true;
73 | }
74 |
75 | abstract protected function validateEach($record, $attribute, $value);
76 |
77 | protected function check_validity()
78 | {
79 |
80 | }
81 |
82 |
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/lib/Lycan/Validations/Validators/Acceptance.php:
--------------------------------------------------------------------------------
1 |
27 | * $options = array('Acceptance' => true);
28 | * $this->validates('terms_of_service', $options);
29 | *
30 | * $options = array('Acceptance' => array(
31 | * 'message'=> 'accept the terms or else ...',
32 | * 'if' => function($class){
33 | * return !$class->isNeedToAccept;
34 | * }
35 | * ));
36 | *
37 | * $this->validates('terms_of_service',$options);
38 | *
39 | *
40 | * @vendor Lycan
41 | * @package Validations
42 | * @author Andreas Kollaros
79 | * $validations = array('Validator' => array('message'=>'my error message'));
80 | *
81 | * $validations = array('Validator' => true); // you must pass true if
82 | * options are not exist.
83 | *
84 | *
85 | * On examples above the Lycan\Validations\Validators\Validator will be
86 | * called.
87 | *
88 | * @params string|array $attrs the properties of class to validate
89 | * @params array $validations an array of Validator name class and
90 | * its options.
91 | *
92 | * @throws \Exception if a Validator class does not exist.
93 | *
94 | * @return void
95 | */
96 | final public function validates($attrs, array $validations)
97 | {
98 | foreach ($validations as $key=>$options) {
99 |
100 | $validator = "\\Lycan\\Validations\\Validators\\" . $key;
101 |
102 | if (!class_exists($validator)) {
103 | throw new \Exception("Unknown validator: {$key}");
104 | }
105 |
106 | $defaults = $this->_parse_validates_options($options);
107 | $defaults['attributes'] = $attrs;
108 | $vtor = new $validator($defaults);
109 | $vtor->validate($this);
110 | }
111 | }
112 |
113 | public function readAttributeForValidation($attribute)
114 | {
115 | return isset($this->$attribute) ? $this->$attribute : null;
116 | }
117 |
118 | protected function validations()
119 | {
120 | return true;
121 | }
122 |
123 | protected function run_validations()
124 | {
125 | $this->validations();
126 |
127 | return $this->errors()->count() == 0;
128 | }
129 |
130 | private function _parse_validates_options($options)
131 | {
132 | if (is_array($options)) {
133 |
134 | return $options;
135 | } elseif(is_bool($options)) {
136 |
137 | return array();
138 | } else {
139 |
140 | return array('with' => $options);
141 | }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/lib/Lycan/Validations/Validators/Numericality.php:
--------------------------------------------------------------------------------
1 | '>',
11 | 'greater_than_or_equal_to' => '>=',
12 | 'equal_to' => '==',
13 | 'less_than' => '==',
14 | 'less_than_or_equal_to' => '<=',
15 | 'odd' => 'odd',
16 | 'even' => 'even',
17 | 'other_than' => '!='
18 | );
19 |
20 | private $_reserved_options = array(
21 | 'greater_than' => '>',
22 | 'greater_than_or_equal_to' => '>=',
23 | 'equal_to' => '==',
24 | 'less_than' => '<',
25 | 'less_than_or_equal_to' => '<=',
26 | 'odd' => 'odd',
27 | 'even' => 'even',
28 | 'other_than' => '!=',
29 | 'only_integer' => null
30 | );
31 |
32 | protected function validateEach($record, $attribute, $value)
33 | {
34 | if (array_key_exists('allow_null',$this->options)) {
35 | if ($this->options['allow_null'] && null === $value)
36 | return;
37 | }
38 |
39 | if (array_key_exists('only_integer',$this->options)) {
40 | if ($this->options['only_integer']) {
41 | if (!($value = $this->parse_value_as_integer($value))) {
42 | $record->errors()->add($attribute, ':not_an_integer', $this->filtered_options($value));
43 | return;
44 | }
45 | }
46 | }
47 | $options = array_intersect_key($this->options, $this->checks);
48 | foreach ($options as $option => $option_value) {
49 | switch ($option) {
50 | case 'odd':
51 | if ( 1 !== (1 & $value))
52 | $record->errors()->add($attribute, ":$option", $this->filtered_options($value));
53 | break;
54 | case 'even':
55 | if ( 0 !== (1 & $value))
56 | $record->errors()->add($attribute, ":$option", $this->filtered_options($value));
57 | break;
58 | default:
59 |
60 | if ( is_callable($option_value)) $option_value = $option_value($record);
61 |
62 | if ( false === $this->_check_value($value, $option_value, $this->checks[$option])) {
63 | $o = $this->filtered_options($value);
64 | $o['count'] = $option_value;
65 | $record->errors()->add($attribute, ":$option", $o);
66 | }
67 | break;
68 | }
69 | }
70 |
71 | }
72 |
73 | protected function check_validity()
74 | {
75 | $options = array_intersect_key($this->options, $this->checks);
76 | foreach ($options as $option=>$value) {
77 | if ($option == 'odd' || $option == 'even' || is_numeric($value) || is_callable($value)) continue;
78 | throw new \InvalidArgumentException("{$option} must be a number or a function");
79 | }
80 | }
81 |
82 | protected function parse_value_as_number($value)
83 | {
84 | if ( is_numeric($value)) {
85 | if ( is_float($value) ) return (float) $value;
86 | if ( is_int($value) ) return (int) $value;
87 | }
88 | }
89 |
90 | protected function parse_value_as_integer($value)
91 | {
92 | if ( is_numeric($value) && is_int($value))
93 | return (int) $value;
94 | else
95 | return null;
96 | }
97 |
98 | protected function filtered_options($value)
99 | {
100 | $options = array_diff_key($this->options, $this->_reserved_options);
101 | $options['value'] = $value;
102 | return $options;
103 | }
104 |
105 | private function _check_value($record_value, $check_value, $operator)
106 | {
107 | switch ($operator) {
108 | case '>':
109 | return $record_value > $check_value;
110 | break;
111 | case '<':
112 | return $record_value < $check_value;
113 | break;
114 | case '==':
115 | return $record_value == $check_value;
116 | break;
117 | case '>=':
118 | return $record_value >= $check_value;
119 | break;
120 | case '<=':
121 | return $record_value <= $check_value;
122 | break;
123 | }
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/lib/Helper/Html.php:
--------------------------------------------------------------------------------
1 | ';
42 | }
43 |
44 | public static function linkTo($text, $url, $attrs = array())
45 | {
46 | return '' . $text . '';
47 | }
48 |
49 | public static function span($content, $attrs=array())
50 | {
51 | return ''.self::encode($content)."";
52 | }
53 |
54 | public static function getEncoding()
55 | {
56 | if (self::$encoding) {
57 | return self::$encoding;
58 | }
59 |
60 | self::$encoding = 'utf-8';
61 |
62 | return self::$encoding;
63 | }
64 |
65 | public static function validUrl($url)
66 | {
67 | if (null === $url || "" == $url)
68 | return null;
69 |
70 | return substr($url, 0, 4) == "http" ? $url : "http://" . $url;
71 | }
72 |
73 | public static function javascript($url)
74 | {
75 | return '' . PHP_EOL;
76 | }
77 |
78 | public static function css($url, $attrs=array())
79 | {
80 | $defaults = array('media'=>'screen', 'type'=>'text/css', 'rel'=>'stylesheet');
81 |
82 | $attrs = array_merge($attrs, $defaults);
83 |
84 | return '' . PHP_EOL;
85 | }
86 |
87 | /**
88 | * Appends a javascript file from current view.
89 | *
90 | * Some javascript files are required to load only in specific action
91 | * views. You can add them with this method.
92 | *
93 | * Notice that you have to add a line to your layout file like:
94 | *
95 | * = $this->javascripts ?>
96 | *
97 | * This is the place where additional javascript files would appear.
98 | *
99 | * @param View_Interface $view The view instance
100 | * @param string|array $args An array of javascript paths or a
101 | * string with javascript path to load.
102 | */
103 | public static function appendJavascript(ViewInterface $view, $args)
104 | {
105 | if ( !isset($view->javascripts)) {
106 | $view->assign('javascripts', '');
107 | }
108 |
109 | if ( !is_array($args)) {
110 | $args = array($args);
111 | }
112 |
113 | foreach ($args as $arg) {
114 | $url = strpos($arg, 'http') === 0 || strpos($arg, '/') === 0
115 | ? $arg
116 | : '/javascripts/'.$arg;
117 | $view->javascripts .= self::javascript($url . '.js');
118 | }
119 | }
120 |
121 | /**
122 | * Appends a stylesheet file from current view.
123 | *
124 | * Some stylesheet files are required to load only in specific action
125 | * views. You can add them with this method.
126 | *
127 | * Notice that you have to add a line to your layout file like:
128 | *
129 | * = $this->css ?>
130 | *
131 | * This is the place where additional stylesheet files would appear.
132 | *
133 | * @param View_Interface $view The view instance
134 | * @param string|array $args An array of stylesheet paths or a
135 | * string with stylesheet path to load.
136 | */
137 | public static function appendCss(ViewInterface $view, $args)
138 | {
139 | if ( !isset($view->stylesheets)) {
140 | $view->assign('stylesheets', '');
141 | }
142 |
143 | if ( !is_array($args)) {
144 | $args = array($args);
145 | }
146 |
147 | foreach ($args as $arg) {
148 | $url = strpos($arg, 'http') === 0 || strpos($arg, '/') === 0
149 | ? $arg
150 | : '/stylesheets/'.$arg;
151 | $view->stylesheets .= self::css($url.'.css');
152 | }
153 | }
154 |
155 | public static function attributes($array)
156 | {
157 | if (empty($array)) return null;
158 |
159 | $o = "";
160 | foreach ($array as $k => $v) {
161 | if ( null !== $v )
162 | $o .= ' ' . $k . '="' . self::encode($v) . '"';
163 | }
164 | return ' '.$o;
165 | }
166 |
167 | }
168 |
--------------------------------------------------------------------------------
/app/controllers/Restfull.php:
--------------------------------------------------------------------------------
1 | getResourceCollection();
34 | $this->getView()->assign('collection', $collection);
35 | }
36 |
37 | /**
38 | * Loads and displays a single resource asccording to Request::getParams()
39 | * values.
40 | *
41 | * By default the uri to show a resource is /module/controller/show/id/1
42 | *
43 | * @param int $id The id of resource to load
44 | */
45 | public function showAction($id)
46 | {
47 | $resource = $this->getResource($id);
48 |
49 | if (null === $resource) {
50 | return $this->forwardTo404();
51 | } else {
52 | $this->getView()->assign('resource', $resource);
53 | }
54 | }
55 |
56 | public function newAction()
57 | {
58 | $model = $this->get_model_name();
59 | $resource = new $model();
60 | $this->get_index_url();
61 | $this->getView()->assign('resource', $resource);
62 | }
63 |
64 | public function createAction()
65 | {
66 | $model = $this->get_model_name();
67 |
68 | $resource = new $model($this->get_resource_params($model));
69 |
70 | if ($resource->save()) {
71 | $this->redirect($this->get_index_url());
72 | return false;
73 | } else {
74 | Yaf\Dispatcher::getInstance()->disableView();
75 | echo $this->render('new', array('resource'=>$resource,
76 | 'index_url'=>$this->get_index_url()));
77 | return false;
78 | }
79 | }
80 |
81 | public function editAction($id)
82 | {
83 |
84 | $resource = $this->getResource($id);
85 |
86 | if (null === $resource) {
87 | return $this->forwardTo404();
88 | } else {
89 | $this->get_index_url();
90 | $this->getView()->assign('resource', $resource);
91 | }
92 | }
93 |
94 | public function updateAction($id)
95 | {
96 | $resource = $this->getResource($id);
97 |
98 | if (null === $resource) {
99 | return $this->forwardTo404();
100 | }
101 |
102 | $model = $this->get_model_name();
103 |
104 | if ($resource->updateAttributes($this->get_resource_params($model))) {
105 | $this->redirect($this->get_index_url());
106 | return false;
107 | } else {
108 | Yaf\Dispatcher::getInstance()->disableView();
109 | echo $this->render('edit', array('resource'=>$resource,
110 | 'index_url'=>$this->get_index_url()));
111 | return false;
112 | }
113 | }
114 |
115 | public function deleteAction()
116 | {
117 | $resource = $this->getResource();
118 |
119 | if (null === $resource) {
120 | return $this->forwardTo404();
121 | }
122 |
123 | $this->resource->destroy();
124 |
125 | $this->redirect($this->get_index_url());
126 | return false;
127 | }
128 |
129 | /**
130 | * Creates a resource collection.
131 | *
132 | * By default it finds all elements of the resource. You can overload this
133 | * methods to return custom queries of the resource like pagination and/or
134 | * search.
135 | *
136 | * @return Orm\Mysql\Collection A collection object handling elements of
137 | * the resource
138 | */
139 | public function getResourceCollection()
140 | {
141 | $model = $this->get_model_name();
142 |
143 | return $model::find()->fetchAll();
144 | }
145 |
146 | /**
147 | * Creates a resource by a given id.
148 | *
149 | * By default it finds the resource for the given id.
150 | * Overload this method if you want to load a resource with additional
151 | * criteria.
152 | *
153 | * @param int $id The id of resource to load.
154 | */
155 | public function getResource($id)
156 | {
157 | if (null === $this->_resource) {
158 | $model = $this->get_model_name();
159 | $this->_resource = $model::findById($id)
160 | ->fetch();
161 | }
162 | return $this->_resource;
163 | }
164 |
165 | protected function get_model_name()
166 | {
167 | return Inflect::singularize($this->getRequest()->getControllerName())
168 | . "Model";
169 | }
170 |
171 | protected function get_resource_params($model)
172 | {
173 | $param_name = Inflect::underscore(str_replace('Model', '', $model));
174 | $post = $this->getRequest()->getPost();
175 |
176 | return $post[$param_name];
177 | }
178 |
179 | protected function get_index_url()
180 | {
181 |
182 | if (null !== $this->_index_url) {
183 | return $this->_index_url;
184 | }
185 |
186 | $module = $this->getRequest()->getModuleName();
187 |
188 | $this->_index_url = "/"
189 | . ('index' == strtolower($module) ? null : $module. "/")
190 | . Inflect::underscore($this->getRequest()->getControllerName())
191 | . "/index";
192 |
193 | $this->getView()->assign('index_url', $this->_index_url);
194 | return $this->_index_url;
195 | }
196 | }
197 |
--------------------------------------------------------------------------------
/lib/eYaf/Layout.php:
--------------------------------------------------------------------------------
1 |
17 | * application/views/layouts/front.phtml
18 | *
19 | *
20 | *
21 | *
36 | * application/views/index/index.phtml
37 | *
38 | * title = "Index Action" ?>
39 | * Index Action
40 | *
41 | *
42 | * @author Andreas Kollaros