silexphp / Silex

[DEPRECATED -- Use Symfony instead] The PHP micro-framework based on the Symfony Components
https://silex.symfony.com
MIT License
3.58k stars 718 forks source link

split controllers in more file #1308

Closed giammbo closed 8 years ago

giammbo commented 8 years ago

Hi im using silex php framework,

i have 2 file:

the first is the app.php with this:

<?php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use Silex\ControllerProviderInterface;
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
    'driver'    => 'pdo_mysql',
    'host'      => 'localhost',
    'dbname'    => 'testapp',
    'user'      => 'root',
    'password'  => 'turoke55u',
    ),
));
$app->before(function (Request $request) {
     if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->request->replace(is_array($data) ? $data : array());
    }
});
$app->mount('/', include 'login.php');
$app->run();

and the second is the login.php:

<?php
use Symfony\Component\HttpFoundation\Request;
$login = $app['controllers_factory'];
$login->post('/apiv1/user/login', function (Request $request) use ($login) {
    $userinfo = array(
        'email'  => $request->request->get('email'),
        'mode'  => $request->request->get('mode'),
        'password'  => $request->request->get('password'),
    );
    $passwordcoding = sha1($userinfo['email']."66643asd");
    $emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'";
    $selectemail = $login['db']->$fetchAll('SELECT * FROM user');
    var_dump($emailverification);
});
return $login;

when i run the select on db i receive this error:

Cannot use object of type Silex\ControllerCollection as array in /mnt/hgfs/Share_Folder/frontend/src/login.php on line 13

solutions?

and the second questions is why if i change in app.php from this

$app->mount('/', include 'login.php');

to this:

$app->mount('/apiv1/user/login', include 'login.php');

and in the login.php

$login->post('/',

like the silex documentation, the framework doesnt works??

thanks.

clemgrim commented 8 years ago

Hello, your $login variable is not the same as your $app variable. As the error message suggests, your login var is an instance of ControllerCollection class, it is not an Application or a Pimple one.

You have to use $app instead of $login in your controller function

$login->post('/apiv1/user/login', function (Request $request) use ($app) {
    $userinfo = array(
        'email'  => $request->request->get('email'),
        'mode'  => $request->request->get('mode'),
        'password'  => $request->request->get('password'),
    );
    $passwordcoding = sha1($userinfo['email']."66643asd");
    $emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'";
    $selectemail = $app['db']->$fetchAll('SELECT * FROM user');
});
giammbo commented 8 years ago

i have changed the second file (login.php) like this: <?php $app = $app['controllers_factory']; $app->post('/', function (Request $request) use ($app) { $userinfo = array( 'email' => $request->request->get('email'), 'mode' => $request->request->get('mode'), 'password' => $request->request->get('password'), ); $passwordcoding = sha1($userinfo['email']."66643asd"); $emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'"; $selectemail = $app['db']->$fetchAll('SELECT * FROM user'); var_dump($emailverification); }); return $app;

and now i receive this error:

[Sun Jan 17 05:01:27 2016] [error] [client 192.168.13.1] PHP Warning:  Missing argument 1 for Silex\\Route::run() in /mnt/hgfs/Share_Folder/frontend/vendor/silex/silex/src/Silex/Route.php on line 51
[Sun Jan 17 05:01:27 2016] [error] [client 192.168.13.1] PHP Notice:  Undefined variable: to in /mnt/hgfs/Share_Folder/frontend/vendor/silex/silex/src/Silex/Route.php on line 53
[Sun Jan 17 05:01:27 2016] [error] [client 192.168.13.1] PHP Warning:  Missing argument 1 for Silex\\Route::run() in /mnt/hgfs/Share_Folder/frontend/vendor/silex/silex/src/Silex/Route.php on line 51
[Sun Jan 17 05:01:27 2016] [error] [client 192.168.13.1] PHP Notice:  Undefined variable: to in /mnt/hgfs/Share_Folder/frontend/vendor/silex/silex/src/Silex/Route.php on line 53
clemgrim commented 8 years ago
$app = $app['controllers_factory'];

This is wrong, you are overriding your app variable.

Use $app variable only inside your controller function (use $app as a container), and for your factory creation. Use $login to create your route.

$login= $app['controllers_factory'];
$login->post('/', function (Request $request) use ($app) {
    $userinfo = array(
        'email' => $request->request->get('email'),
        'mode' => $request->request->get('mode'),
        'password' => $request->request->get('password'),
    );
    $passwordcoding = sha1($userinfo['email']."66643asd");
    $emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'";
    $selectemail = $app['db']->$fetchAll('SELECT * FROM user');
    var_dump($emailverification);
});
return $login;
giammbo commented 8 years ago

ok works but, now the app.php have this code:

$app->mount('/', include __DIR__.'/login.php');

and the in the login.php

use Symfony\Component\HttpFoundation\Request;<-- i needed to add this
$login->post('/apiv1/user/login', function (Request $request) use ($app) {

in the documentation it's write like this: app.php

 $app->mount('/apiv1/user/login', include __DIR__.'/login.php');

login.php

 $login->post('/', function (Request $request) use ($app) {

but if i write like the documentation i recieve a 404 only on a post request the get works fine.why?

mediamonks-robert commented 8 years ago

Because the "post" in "$login->post()" sets the allowed method for that route to POST. The documentation explains this pretty well: http://silex.sensiolabs.org/doc/usage.html#routing

PS. imo you are misusing this issue tracker as a method to get help on implementation. I suggest you read the documentation carefully and join us on freenode irc in channel #silex-php if you have some more implementation questions. We are happy to help you :)

giammbo commented 8 years ago

ok thanks. you can close this issue

ragboyjr commented 8 years ago

Can someone close this issue?