This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

alexwenzel
alexwenzel

Hi all,

I have a custom controller inside my plugin. The routing is done by my plugins 'routes.php' file.

Is it possible to return a page/view from a theme by my controller method?

Example:

class MyController extends Controller
{
    public function showItem($id)
    {
        return view('myTheme::pages.item_single');
    }
}

more explanation:

I have my own theme developed and a plugin with frontend logic (frontend controllers).

I have several cms pages (theme related) but I also want to have custom routing (plugin related).

Now i need a way to connect both. Means I want to return a theme layout from a custom route inside my plugins routes.php

If this is not possible: any ideas how to solve this problem in a different way?

thanks, alex :)

Last updated

JeffGoldblum
JeffGoldblum

You could trigger the run method on the CMS controller class directly and setup a layout to respond to a route

$cmsController = new Cms\Classes\Controller;
return $cmsController->run('/url/to/my/layout');

but honestly I don't think you should be introducing dependancies on themes from your plugin - that's backwards of what should be happening.

What you should be doing is having your plugin utilize components to provide data to your theme, which you can then setup to display however you like in whatever routing you'd like. If you have any questions about setting up routes in layouts in combination with plugin components, then ask away.

I really highly recommend not depending on a specific theme by attempting to render its layout from your plugin though.

goldnetonline
goldnetonline

I have exactly 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

Using route.php in plugin I was able to do the following:

// Last fail over for looking up slug from the database
    Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {
            //Pretend this are our routes and I can go to the database to confirm based on
            .// the different scenerio like page or profile
        $routes = ["bola", "sade", "bisi", "ade", "tayo"]; 

        if(in_array($slug, $routes)) {
            // Now I want to render a page from the CMS 
                // and pass some data or url parameter along
        }

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

I have tried

Response::view('namespace.plugin::file', ['sulg' => $slug]);

But the above cannot utilize the themes layout, styles and page configurations.

If you have any other idea on how to achieve this better, please let me know

Response will be much appreciated

goldnetonline
goldnetonline

A workaround (Worked for October v434).

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

Then I 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.

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.