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

kris2481
kris2481

I am attempting to implement a RESTful Resource controller based on the laravel docs. By default the resource controller is an extension of BaseController class in laravel. But it appears that this base class does not exist with October?

Is there a better way that I should be utilizing to implement a RESTful resource controller?

delphinium
delphinium

Did you ever find an answer to this question? I'm trying to do something similar...

shina
shina

I was looking for a functionality like this too. But after some thoughts, I wonder if October is the right tool to do it.

Maybe you can do the opposite, sharing the Models classes with another lightweight framework. eg: SlimFramework or Silex

What do you guys think about?

that0n3guy
that0n3guy

I need to test this out, but I think you can still easily do this, (I'm trying to remember if I already did this in a project on october already).

I think you can do any laravel route in your plugins route.php... something like:

Route::get('webforms/submit/{secret}', array('as' => 'webform.api.post', 'uses' => 'OCA\Webforms\Classes\Ocaforms@getWebform'));
Route::post('webforms/submit/{secret}', array('as' => 'webform.api.post', 'uses' => 'OCA\Webforms\Classes\Ocaforms@storeWebform'));

Then you can do this in a class:

<?php namespace OCA\Webforms\Classes;

use Illuminate\Support\Facades\Input;
use OCA\Webforms\Models\Formposts;
use OCA\Webforms\Models\Ocaform as OcaformsModel;

class Ocaforms extends \Controller
{

    private $rules = [
      //'ocaform_id' => 'required',
    ];

    private $ocaform;

    private $post;

    public function store($secret)
    {
        $this->ocaform = OcaformsModel::where('secret', '=', $secret)->first();
        if(! $this->ocaform){
            \Redirect::back();
            //@todo display error
        }

The "base controller" your talking about just extends Controller like I do above. Or you could use an use Controller; and then you don't need the \ in front of Controller.

You will do the same thing but w/ Route::resource(' like explained in the laravel docs.

Last note, maybe take a read this decided if you even want to use the automatic routes: https://philsturgeon.uk/blog/2013/07/beware-the-route-to-evil

chris.vanderpoel7523
chris.vanderpoel7523

Just in case anyone is trying to do this.

<?php namespace Your\Namespace\Classes;

use Cms\Classes\Controller;

// This would reside in your plugin's "classes" directory
class Custom extends Controller
{
    public function index()
    {
        // You should be able to hit this...
        dd('You are here.');
    }
}
# Your plugin's routes.php might have something like this
Route::get('/testing', [
    'as' => 'testingroute',
    'uses' => 'Your\Namespace\Classes\Custom@index'
]);

So you should be able to hit that route at: http://your.installation.domain/testing

I should be clear though... that's just how I managed to get it working. Not sure if it's best practice or not (would love some developer feedback).

This is something that should be documented. There are plenty of use cases for it and I wish there was an easier, more best-practice way of implementing functionality (and routing) that needs to be separate from the October application. For instance, we're building a custom checkout flow that is very independent of what October is and should be handling however we want to leverage the domain models that have been created with October and additionally be able to leverage some of Laravel's features. While we know that components are the front-end "controllers" in October, they're too tightly coupled with the backend and require being attached to something to have a route.

Last updated

kuba.markiewicz17483
kuba.markiewicz17483

Thanks Chris, your solution worked, however I had to change the controller to extend Illuminate\Routing\Controller instead of Cms\Classes\Controller

Mohsin
Mohsin

For anyone else trying to do this, check out my plugin

1-7 of 7

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