2
votes

I'm wondering how I can render a view, or display a page with my default theme in OctoberCMS, via a route that executes a function in a controller.

If I have the following route:

Route::get('bransje', [
'uses' => 'Ekstremedia\Cityportal\CPController@bransje'
]);

And in my controller CPController ive tried several things, like I used to with Laravel:

public function bransje() {
    $stuff = Stuff::with('info');
    return View::make('cms::bransje')->with('stuff',$stuff);
}

But I cannot seem to get it to work, and I've tried to search the web, but it's hard to find answers. I have found a workaround, and that is to make a plugin component, then I can include that component and do:

public function onRun()
{
    $this->eventen = $this->page['stuff'] = $this->stuff();
}

protected function stuff()
{
   return ...
}

Is there any way so I can make pages without using the Cms, and that are wrapped in my default theme? I've tried

return View::make('my-theme-name::page');

and a lot of variants but no luck.

I know I can also do a:

==
public function onRun()
{
}

in the start of my page in the cms, but I'm not sure how to call a function from my plugin controller via there.

2

2 Answers

1
votes

You can bypass frontend routing by using routes.php file in your plugin.

Full example in this video turotial.

0
votes

If this answer can still be useful (Worked for October v434).

I have almost the same scenerio.

What I want to achieve is a type of routing like facebook page and profile.

facebook.com/myprofile is the same url structure as facebook.com/mypage

First I create a page in the CMS for each scenario (say catchpage.htm)

Then a created a catchall route at the buttom of routes.php in my plugin that will also not disturb the internal working of octobercms.

if (!Request::is('combine/*') && !Request::is('backend/*') && !Request::is('backend')) {

    // Last fail over for looking up slug from the database
    Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {

       //Pretend this are our routes and we can check them against the database 
        $routes = ["bola", "sade", "bisi", "ade", "tayo"];

        if(in_array($slug, $routes)) {

            $cmsController = new Cms\Classes\Controller;
            return $cmsController->render("/catchpage", ['slug' => $slug]);

        }

        // Some fallback to 404
        return Response::make(View::make('cms::404'), 404);
    });
}

The if Request::is check is a list of all the resource that october uses under the hood, please dont remove the combine as it is the combiner route. Remove it and the style and script will not render. Also the backend is the url to the backend, make sure to supply the backend and the backend/*.

Finally don't forget to return Response::make(View::make('cms::404'), 404); if the resource is useless.

You may put all these in a controller though.

If anyone has a better workaround, please let us know.