Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjardim/119418eb500110ed8cc4313166477c21 to your computer and use it in GitHub Desktop.
Save sjardim/119418eb500110ed8cc4313166477c21 to your computer and use it in GitHub Desktop.
[Support nested urls for nested modules] #twill

By default Twill will disply the url of a module item to be {siteurl}/{module}/{slug} e.g. mysite.com/pages/about. With the 'about' section being the editable slug. This is understandable as Twill knows nothing of your frontend routing. However it is likely confusing for editors, as that url presented will not be the final url of the content.

Remove module name form url

This will give us more correct urls e.g. mysite.com/about

// app/Http/Controllers/Admin/PageController

	protected $permalinkBase = '';

Allow for nested pages to have correct urls

To achieve this we need to set permalinkBase dynamically with a function.

// app/Http/Controllers/Admin/PageController

	protected $permalinkBase = '';

	public function __construct(Application $app, Request $request) {
        parent::__construct($app, $request);

        $this->permalinkBase = $this->getPermalinkBase($request);
    }


	protected function getPermalinkBase($request) {
        $param = strtolower($this->modelName);
        $id = $request->route($param);
        
      	if( !$id ) {
            return  $this->permalinkBase;
        }

        $item = $this->repository->getById($id);
        if( !$item->isRoot() ) {
            $permalinkBase = '';
            $ancestors = $item->ancestors()->with('slugs')->get();
            foreach($ancestors as $ancestor) {
                $permalinkBase .= '/'. $ancestor->slug;
            }
            //remove first slash and return
            return substr( $permalinkBase . $this->permalinkBase, 1);
        } else {
            return  $this->permalinkBase;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment