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

john1420
john1420

Ok, so I am creating a simple restful webservice as part of the site I am making with October. This webservice isn't running through the "application" part of October, since the content type needs to be xml. For this webservice, I would greatly like to use some of the models I have defined in my app/models directory. However, I cannot seem to load the October Model class or all of its dependencies.

What is the "right" way to load the October/Laravel Eloquent/Model part of the framework? How would I go about doing this? I appreciate both specific examples and general suggestions.

adis.osmonov
adis.osmonov

I'm not sure, but just add your models directory to composer's autoload. And don't forget to use namespaces.

john1420
john1420

I think you misunderstand. I don't need/want to autoload my app/models directory, I want to load October's Model portion of the framework. I can access my models just by including them, but they extend Model, which means I need to access that somehow. That's where my problem lies. Even if I require Model, I don't have a connection set up. Assuming I figure that out at some point, there may be other problems I run into. It would be nice if there were a set of steps/pieces of code I could use that would load all the Model and Model-related code, without needing to dissect the framework as a whole.

john1420
john1420

Ok, so here's what I made that works. I don't know that I need all of those "initialization" steps, and if anyone wants to contribute a cleaner way to do this, I would be grateful.

require 'vendor/autoload.php';

October\Rain\Support\ClassLoader::register();
October\Rain\Support\ClassLoader::addDirectories(array('modules', 'plugins'));

Illuminate\Support\ClassLoader::register();

$app = new Illuminate\Foundation\Application;

$env = $app->detectEnvironment(function(){
    return isset($_SERVER['CMS_ENV']) ? $_SERVER['CMS_ENV'] : null;
});

$app->bindInstallPaths(require 'bootstrap/paths.php');

$framework = 'vendor/laravel/framework/src';
require $framework.'/Illuminate/Foundation/start.php';

$config = require 'app/config/database.php';
$mysqlConfig = $config['connections']['mysql'];
$connector = new Illuminate\Database\Connectors\MySqlConnector();
$pdo = $connector->connect($mysqlConfig);

$connection = new Illuminate\Database\MySqlConnection($pdo);
$connectionResolver = new Illuminate\Database\ConnectionResolver(['default'=>$connection]);
$connectionResolver->setDefaultConnection('default');
Illuminate\Database\Eloquent\Model::setConnectionResolver($connectionResolver);

Last updated

ChadStrat
ChadStrat

I'm a little confused why you wouldn't just load a custom route? We do that for XML rss and ical feeds and it's fantastic.

just add a routes.php to your package and use something similar to this:

use Radiantweb\Proevents\Classes\IcalManager;
Route::group(['prefix' => 'radiantweb_api/events/ical/'], function() {
    Route::get('all', function(){ return View::make('radiantweb.proevents::ical.all', ['events' => IcalManager::getAllEvents()]); });
    Route::get('calendar', function(){ return View::make('radiantweb.proevents::ical.calendar', []); });
    Route::get('event', function(){ return View::make('radiantweb.proevents::ical.event', []); });
});

Last updated

john1420
john1420

I put a routes.php under the app directory at the root, and added the following:

Route::get('/rest.php', function()
{
    echo "test!";
    return 'Hello World';
});

However, I just got a 404 error when trying to go to that page. Perhaps there is something I misunderstood about routes?

Last updated

1-6 of 6

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