Container Resolution
Similar to Controllers Help
I’m trying to use Container Resolution to keep my index.php page from getting too big.
I’m definitely a beginner to Slim. And I’m trying to follow the instructions from :
http://www.slimframework.com/docs/objects/router.html#container-resolution
But I’m missing something.
When I go to {domain_name}/method1 in my browser, I get the error message :
Slim Application Error
A website error has occurred. Sorry for the temporary inconvenience.
However, {domain_name}/org works fine.
For the /org route I’m using the “require_once” option (see “org.php” below). While this will help to keep the index.php file small, I would rather use Container Resolution.
Like I said, I know I’m missing something obvious.
Can someone help me?
Thanks
Below is information on my file structure and relevant files
FOLDER STRUCTURE :
/usr/share/nginx/html
index.php
MyController.php
org.php
/src
autoload.php << #1 >>
/vendor
autoload.php << #2 >>
/composer
/container-interop
/nikic
/pimple
/psr
/slim
index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$app = new \Slim\App();
// NOT Working ...
$app->get('/method1', '\MyController:method1');
// Working!
require_once('org.php');
$app->run();
-------------------------------
**_MyController.php_**
-------------------------------
<?php
class MyController {
protected $ci;
//Constructor
public function __construct(ContainerInterface $ci) {
$this->ci = $ci;
}
public function method1($request, $response, $args) {
/* NONE OF THE METHODS BELOW WORK */
// return $response->write("Method1");
// $this-ci->get('Method1');
// echo "Method1";
}
public function method2($request, $response, $args) {
//your code
//to access items in the container... $this->ci->get('');
}
public function method3($request, $response, $args) {
//your code
//to access items in the container... $this->ci->get('');
}
}
-------------------------------
**_org.php_**
-------------------------------
<?php
$app->get('/org', function()
{
echo json_encode("Your Organization Page");
});
-------------------------------
_**/src/**_
_**autoload.php <>**_
-------------------------------
<?php
require 'vendor/autoload.php';
-------------------------------
_**/vendor/**_
_**autoload.php <>**_
-------------------------------
<?php
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInitc888888ddddafdagdadfagd::getLoader();
Hi Bryan,
It looks as though you might not have created the container, or if you have you are not passing it to your application. See the Application Configuration section of the docs. Specifically look at how the container $config
is being passed to the application.
You may wish to start exploring Slim by having a look at the Slim Skeleton for a little bit more of a working example, then try the code you see in the Container Resolution section of the docs.
Thanks Tim!
I’ll check out the skeleton, and take another look at the App Config section in the docs.