/** * Register opinionated controllers * * @param array $controllers the controllers and config * @param string $bundle the bundle where the controller can be found * @param string $url_prefix a global url prefix * @param string $route_type the type of the route (pages or api) * @param array $parents */ public static function opinionated($controllers, $bundle, $url_prefix, $route_type, $parents = array()) { if (!ends_with($url_prefix, '/')) { $url_prefix .= '/'; } foreach ($controllers as $controller => $options) { list($types, $children) = $options; foreach ($types as $type) { list($method, $plural, $has_identity, $postfix) = static::$types[$type]; $segment = $controller; $action = ($bundle ? $bundle . '::' : '') . implode('.', $parents) . (count($parents) > 0 ? '.' : '') . $segment . '@' . $type; if ($plural) { $segment = Str::plural($segment); } $prefixes = array_map(function ($parent) { return $parent ? $parent . '/(:any)/' : '(:any)/'; }, $parents); $prefix = implode('', $prefixes); $route = $url_prefix . $prefix . $segment . ($has_identity ? '/(:any)' : '') . ($route_type == 'pages' && $postfix ? '/' . $postfix : ''); if ($route_type == 'pages') { $method = '*'; } Router::register($method, $route, $action); } $parents[] = $controller; if (is_array($children)) { static::opinionated($children, $bundle, $url_prefix, $route_type, $parents); } $parents = array(); } }
public static function getModels() { $models = scandir(path('app') . 'models'); $exclude = Config::get('Adminify::settings.exclude'); $exclude = array_merge($exclude, array('.', '..', '.gitignore')); $return = array(); foreach ($models as $model) { $model = ucwords(str_replace('.php', '', $model)); if (!in_array($model, $exclude)) { $return[] = Str::plural($model); } } return $return; }
public function get_edit($model = null, $id = null) { $name = $model; if (is_null($model) || is_null($id)) { return Redirect::to(Adminify\Libraries\Helpers::url('/')); } $model = Helpers::getModel($model); if (is_null($model)) { return Redirect::to(Adminify\Libraries\Helpers::url('/')); } $entry = $model::find($id); $table = property_exists($model, 'table') && !is_null($model::$table) ? $model::$table : strtolower(Str::plural($model)); $structure = DB::query("SHOW COLUMNS FROM `" . $table . "`"); $excluded = Helpers::getFields($model); $this->layout->title = 'Edit ' . $model; $this->layout->nest('content', 'adminify::models.edit', array('entry' => $entry, 'model' => $model, 'name' => $name, 'structure' => $structure, 'excluded' => $excluded)); }
/** * Generates resourceful routes. * * Assumes controller name is plural. * * @param string $name * @param array $include actions to generate routes for * @return void */ public static function resourceful($name, $include = array('index', 'new', 'create', 'show', 'edit', 'update', 'destroy')) { $plural = Str::plural($name); $singular = Str::singular($name); if (in_array('index', $include)) { Router::register("GET", "{$plural}", array("as" => $plural, "uses" => "{$plural}@index")); } if (in_array('new', $include)) { Router::register("GET", "{$plural}/new", array("as" => "new_{$plural}", "uses" => "{$plural}@new")); } if (in_array('create', $include)) { Router::register("POST", "{$plural}", array("as" => $plural, "uses" => "{$plural}@create")); } if (in_array('show', $include)) { Router::register("GET", "{$plural}/(:num)", array("as" => $singular, "uses" => "{$plural}@show")); } if (in_array('edit', $include)) { Router::register("GET", "{$plural}/(:num)/edit", array("as" => "edit_{$singular}", "uses" => "{$plural}@edit")); } if (in_array('update', $include)) { Router::register("PUT", "{$plural}/(:num)", array("as" => $singular, "uses" => "{$plural}@update")); } if (in_array('destroy', $include)) { Router::register("DELETE", "{$plural}/(:num)", array("as" => $singular, "uses" => "{$plural}@destroy")); } if (in_array('destroy', $include)) { Router::register("GET", "{$plural}/(:num)/destroy", array("as" => $singular . "_destroy", "uses" => "{$plural}@destroy")); } }
/** * Get the name of the table associated with the model. * * @return string */ public function table() { return static::$table ?: strtolower(Str::plural(class_basename($this))); }
/** * Determine the intermediate table name for a given model. * * By default, the intermediate table name is the plural names of the models * arranged alphabetically and concatenated with an underscore. * * @param string $model * @return string */ private function intermediate_table($model) { $models = array(Str::plural(static::model_name($model)), Str::plural(static::model_name($this))); sort($models); return Inflector::lower($models[0] . '_' . $models[1]); }
/** * Parse the path to the form or page * * @param string $name * @param string $type * * @return array($file,$class_name,$method) */ protected static function parse($name, $type) { list($path, $method) = explode('@', static::$modules[$type][$name]); list($bundle, $path) = Bundle::parse($path); $file = Bundle::path($bundle) . Str::plural($type) . DS . str_replace('.', DS, $path) . EXT; $class_name = static::format($bundle, $path, $type); return array($file, $class_name, $method); }