Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for arbitrary routes in menus #1023

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Frozennode/Administrator/Config/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class Factory {
*/
protected $pagePrefix = 'page.';

/**
* The route menu prefix
*
* @var string
*/
protected $routePrefix = 'route.';

/**
* The rules array
*
Expand Down Expand Up @@ -124,7 +131,7 @@ public function make($name, $primary = false)
$options = $this->searchMenu($name);

//return the config object if the file/array was found, or false if it wasn't
$config = $options ? $this->getItemConfigObject($options) : ($this->type === 'page' ? true : false);
$config = $options ? $this->getItemConfigObject($options) : (($this->type === 'page' || $this->type === 'route' ) ? true : false);

//set the primary config
$this->config = $primary ? $config : $this->config;
Expand Down Expand Up @@ -176,6 +183,11 @@ public function parseType($name)
{
return $this->type = 'page';
}
//otherwise if the name is prefixed with the route prefix
elseif (strpos($name, $this->routePrefix) === 0)
{
return $this->type = 'route';
}
//otherwise it's a model
else
{
Expand Down Expand Up @@ -242,6 +254,14 @@ public function getPagePrefix()
return $this->pagePrefix;
}

/**
* Gets the prefix for the currently-searched item
*/
public function getRoutePrefix()
{
return $this->routePrefix;
}

/**
* Gets the prefix for the currently-searched item
*/
Expand All @@ -255,6 +275,10 @@ public function getPrefix()
{
return $this->pagePrefix;
}
else if ($this->type === 'route')
{
return $this->routePrefix;
}

return '';
}
Expand Down
6 changes: 3 additions & 3 deletions src/config/administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
/**
* The menu structure of the site. For models, you should either supply the name of a model config file or an array of names of model config
* files. The same applies to settings config files, except you must prepend 'settings.' to the settings config file name. You can also add
* custom pages by prepending a view path with 'page.'. By providing an array of names, you can group certain models or settings pages
* custom pages by prepending a view path with 'page.'. You can also add arbitrary named routes by prepending a route with '.route'. By providing an array of names, you can group certain models or settings pages
* together. Each name needs to either have a config file in your model config path, settings config path with the same name, or a path to a
* fully-qualified Laravel view. So 'users' would require a 'users.php' file in your model config path, 'settings.site' would require a
* 'site.php' file in your settings config path, and 'page.foo.test' would require a 'test.php' or 'test.blade.php' file in a 'foo' directory
* inside your view directory.
* 'site.php' file in your settings config path, 'page.foo.test' would require a 'test.php' or 'test.blade.php' file in a 'foo' directory
* inside your view directory, and 'route.tools' would require a named route 'tools' configured in your routes file.
*
* @type array
*
Expand Down
1 change: 1 addition & 0 deletions src/viewComposers.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
$view->menu = app('admin_menu')->getMenu();
$view->settingsPrefix = app('admin_config_factory')->getSettingsPrefix();
$view->pagePrefix = app('admin_config_factory')->getPagePrefix();
$view->routePrefix = app('admin_config_factory')->getRoutePrefix();
$view->configType = app()->bound('itemconfig') ? app('itemconfig')->getType() : false;
});

Expand Down
5 changes: 4 additions & 1 deletion src/views/partials/menu_item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'item' => $subitem,
'key' => $k,
'settingsPrefix' => $settingsPrefix,
'pagePrefix' => $pagePrefix
'pagePrefix' => $pagePrefix,
'routePrefix' => $routePrefix
))?>
@endforeach
</ul>
Expand All @@ -18,6 +19,8 @@
<a href="{{route('admin_settings', array(substr($key, strlen($settingsPrefix))))}}">{{$item}}</a>
@elseif (strpos($key, $pagePrefix) === 0)
<a href="{{route('admin_page', array(substr($key, strlen($pagePrefix))))}}">{{$item}}</a>
@elseif (strpos($key, $routePrefix) === 0)
<a href="{{route(substr($key, strlen($routePrefix)))}}">{{$item}}</a>
@else
<a href="{{route('admin_index', array($key))}}">{{$item}}</a>
@endif
Expand Down
7 changes: 7 additions & 0 deletions tests/Config/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public function testParseTypeSettings()
$this->assertEquals($factory->getType(), 'settings');
}

public function testParseTypeRoute()
{
$factory = new Factory($this->validator, $this->validator, array());
$factory->parseType('route.something');
$this->assertEquals($factory->getType(), 'route');
}

public function testModelInMenu()
{
$name = 'some_model';
Expand Down