This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

why custom class not invoked by ControllerResolver?

Discuss Symfony 2 bundles here

why custom class not invoked by ControllerResolver?

by ashuma » Tue Oct 21, 2014 10:37 am

hi, i use the symfony 2.5 to build a custom framwork. i built a custom class that named "HomeController";
but when use the createController of ControllerResolver, the createController can not invoke HomeController.

This is the autoload.php below:
Code: Select all
<?php
require HFPATH . 'vendor/autoload.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
   'Config' => HFPATH,
   'DIY\\Controller' => HFPATH . 'components/',
   'DIY\\Module' => HFPATH . 'components/',
));
$loader->register();


The HomeController:
Code: Select all
<?php

namespace DIY\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Config\Template;


class HomeController
{
    public function indexAction($request)
    {
        if (is_leap_year($request->attributes->get('year'))) {
            return new Response('Yep, this is a leap year!');
        }
        return new Response('Nope, this is not a leap year.');
    }
   
    public function show(){
       
          echo 'text';
          exit;
       
    }
}


when i instantiate the HomeController that work for me:

Code: Select all
    /**
     * Returns a callable for the given controller.
     *
     * @param string $controller A Controller string
     *
     * @return mixed A PHP callable
     *
     * @throws \InvalidArgumentException
     */
    protected function createController($controller)
    {
        if (false === strpos($controller, '::')) {
            throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
        }
       
        list($class, $method) = explode('::', $controller, 2);
//             $home = new HomeController();
//             print_r($home);
//            echo $class; //HomeController
        if (!class_exists($class)) {
            throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
        }

        return array(new $class(), $method);
    }


what's happen?

thanks.
ashuma
Member
 
Posts: 61
Joined: Thu Apr 19, 2012 7:42 am