Coder Social home page Coder Social logo

Comments (15)

G4MR avatar G4MR commented on July 3, 2024

Trying to debug this myself locally, in RouteCollection on line 72 I change:

if ($handler instanceof Closure ||
    (is_object($handler) && is_callable($handler))
) {

to

if ($handler instanceof Closure ||
    (is_object($handler) || is_callable($handler))
) {

Fixes the first error, then you get a new set of errors which I didn't get that far with:

Warning: array_key_exists(): The first argument should be either a string or an integer in /var/www/source/vendor/league/container/src/Container.php on line 189

Warning: array_key_exists(): The first argument should be either a string or an integer in /var/www/source/vendor/league/container/src/Container.php on line 194

Warning: Illegal offset type in /var/www/source/vendor/league/container/src/Container.php on line 208

Fatal error: Function name must be a string in /var/www/source/vendor/league/container/src/Container.php on line 210

from route.

philipobenito avatar philipobenito commented on July 3, 2024

Will have a look at this tomorrow morning and ensure all callables are
accepted
On 7 Sep 2015 22:23, "G4MR" [email protected] wrote:

Trying to debug this myself locally, in RouteCollection on line 72 I
change:

if ($handler instanceof Closure ||
(is_object($handler) && is_callable($handler))
) {

to

if ($handler instanceof Closure ||
(is_object($handler) || is_callable($handler))
) {

Fixes the first error, then you get a new set of errors which I didn't get
that far with:

Warning: array_key_exists(): The first argument should be either a string or an integer in /var/www/source/vendor/league/container/src/Container.php on line 189

Warning: array_key_exists(): The first argument should be either a string or an integer in /var/www/source/vendor/league/container/src/Container.php on line 194

Warning: Illegal offset type in /var/www/source/vendor/league/container/src/Container.php on line 208

Fatal error: Function name must be a string in /var/www/source/vendor/league/container/src/Container.php on line 210


Reply to this email directly or view it on GitHub
#61 (comment).

from route.

G4MR avatar G4MR commented on July 3, 2024

I figured out what's wrong or a solution to my issue:

RouteCollection on line 72 I change:

if ($handler instanceof Closure ||
    (is_object($handler) && is_callable($handler))
) {

to

if ($handler instanceof Closure ||
    (is_object($handler) || is_callable($handler))
) {

And in Strategy/AbstractStrategy.php on line 27 if I changed line 27 from:

            $this->getContainer()->get($controller[0]),

to

            $controller[0],

Everything works as expected. Both [$test, 'index'] and 'test::index', now I'm assuming this line creates a new object for the controller class and then attempts to call it normally so you don't have to forcefully call static on the class method. I guess a simple solution would be to check if $controller[0] is already an object?

$controller = [
    is_object($controller[0]) ? $controller[0] : $this->getContainer()->get($controller[0]),
    $controller[1]
];

from route.

philipobenito avatar philipobenito commented on July 3, 2024

It's not actually a static call but a pointer so that the class can be
resolved via the container, your solution looks to be correct but I need to
be sure it fits all scenarios so I'll put together some test cases tomorrow
and get all of them passing.
On 7 Sep 2015 22:44, "G4MR" [email protected] wrote:

I figured out what's wrong or a solution to my issue:

RouteCollection on line 72 I change:

if ($handler instanceof Closure ||
(is_object($handler) && is_callable($handler))
) {

to

if ($handler instanceof Closure ||
(is_object($handler) || is_callable($handler))
) {

And in Strategy/AbstractStrategy.php on line 27 if I changed line 27 from:

        $this->getContainer()->get($controller[0]),

to

        $controller[0],

Everything works as expected. Both [$test, 'index'] and 'test::index', now
I'm assuming this line creates a new object for the controller and then
attempts to call it normally so you don't have to forcefully call static on
the class method. I guess a simple solution would be to check if
$controller[0] is already an object?

$controller = [
is_object($controller[0]) ? $controller[0] : $this->getContainer()->get($controller[0]),
$controller[1]
];


Reply to this email directly or view it on GitHub
#61 (comment).

from route.

G4MR avatar G4MR commented on July 3, 2024

Sounds good.

from route.

philipobenito avatar philipobenito commented on July 3, 2024

Should be fixed by 6029f14

from route.

G4MR avatar G4MR commented on July 3, 2024

Did you forget to update AbstractStrategy.php on Line 27 because I'm still getting:

Warning: array_key_exists(): The first argument should be either a string or an integer in /var/www/source/vendor/league/container/src/Container.php on line 189

Warning: array_key_exists(): The first argument should be either a string or an integer in /var/www/source/vendor/league/container/src/Container.php on line 194

Warning: Illegal offset type in /var/www/source/vendor/league/container/src/Container.php on line 208

Fatal error: Function name must be a string in /var/www/source/vendor/league/container/src/Container.php on line 210

I played with my solution above checking $controller[0] to see if it was an object already seemed to work locally.

Just to post code with the issue again:

<?php

/**
 * ----------------------------
 * Composer Autoloader
 * ----------------------------
 */
require_once dirname(__DIR__) . '/source/vendor/autoload.php';

use League\Route;
use League\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Test {
    public function index(Request $request, Response $response) {
        return new Response("Webpage2", Response::HTTP_OK);
    }
}

$testing = new Test();
$router = new Route\RouteCollection();

$router->addRoute('GET', '/', function(Request $request, Response $resp) {
    $resp->setContent("Testing");
    return $resp;
});

//$router->addRoute('GET', '/test', 'test::index');
$router->addRoute('GET', '/test', [$testing, 'index']);

$dispatcher = $router->getDispatcher();

$request = Request::createFromGlobals();
$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());

$response->send();

from route.

philipobenito avatar philipobenito commented on July 3, 2024

Yep, apologies, I rushed that, will add the check asap

from route.

G4MR avatar G4MR commented on July 3, 2024

alright

from route.

G4MR avatar G4MR commented on July 3, 2024

I noticed you've written another version under the develop branch, when is that expected to drop because it has some features that I've wanted like get,post,etc... to shorten the length of the addRoute method (now map in develop)

from route.

philipobenito avatar philipobenito commented on July 3, 2024

@G4MR expect a tag over the weekend, however, those convenience methods are already there in version 1

from route.

G4MR avatar G4MR commented on July 3, 2024

those convenience methods are already there in version 1

I must have overlooked them, my mistake.

from route.

philipobenito avatar philipobenito commented on July 3, 2024

tag/1.2.3

from route.

G4MR avatar G4MR commented on July 3, 2024

thanks :)

from route.

ericktucto avatar ericktucto commented on July 3, 2024

Hello, I use the magic method __invoke to use the instance of the class to group routes

<?php
use League\Route\RouteGroup;

abstract class Router
{
  abstract protected function routes(RouteGroup $group);
  public function __invoke(RouteGroup $group)
  {
    $this->routes($group);
  }
}

class UserRoutes extends Router
{
  protected function routes(RouteGroup $group)
  {
    $group->post("/users", "UserController::index");
  }
}
$router  = new League\Route\Router;
$request = new Request; // Psr\Http\Message\ServerRequestInterface

// Using UserRoutes
$router->group("/api", new UserRoutes());

$response = $router->dispatch($request);

from route.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.