Code Monkey home page Code Monkey logo

Comments (23)

izniburak avatar izniburak commented on August 16, 2024 2

I've just released a new version that contains some fixes related to the issue. You can update your package via composer!

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024 1

This is my folder.
image

index.php

<?php
require_once(__DIR__.'/vendor/autoload.php');
$router = new Buki\Router([
    'base_folder' => __DIR__
]);
$router->get('/',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->get('/test',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->run();

this is result

object(Buki\Router)#3 (10) {
  ["baseFolder":protected]=>
  string(20) "D:\Xampp\htdocs\demo"
  ["routes":protected]=>
  array(2) {
    [0]=>
    array(7) {
      ["route"]=>
      string(6) "/demo/"
      ["method"]=>
      string(3) "GET"
      ["callback"]=>
      object(Closure)#2 (1) {
        ["static"]=>
        array(1) {
          ["router"]=>
          *RECURSION*
        }
      }
      ["name"]=>
      NULL
      ["before"]=>
      NULL
      ["after"]=>
      NULL
      ["group"]=>
      NULL
    }
    [1]=>
    array(7) {
      ["route"]=>
      string(10) "/demo/test"
      ["method"]=>
      string(3) "GET"
      ["callback"]=>
      object(Closure)#4 (1) {
        ["static"]=>
        array(1) {
          ["router"]=>
          *RECURSION*
        }
      }
      ["name"]=>
      NULL
      ["before"]=>
      NULL
      ["after"]=>
      NULL
      ["group"]=>
      NULL
    }
  }
  ["groups":protected]=>
  array(0) {
  }
  ["patterns":protected]=>
  array(12) {
    ["{a}"]=>
    string(7) "([^/]+)"
    ["{d}"]=>
    string(5) "(\d+)"
    ["{i}"]=>
    string(5) "(\d+)"
    ["{s}"]=>
    string(5) "(\w+)"
    ["{u}"]=>
    string(10) "([\w\-_]+)"
    ["{*}"]=>
    string(4) "(.*)"
    [":id"]=>
    string(5) "(\d+)"
    [":number"]=>
    string(5) "(\d+)"
    [":any"]=>
    string(7) "([^/]+)"
    [":all"]=>
    string(4) "(.*)"
    [":string"]=>
    string(5) "(\w+)"
    [":slug"]=>
    string(10) "([\w\-_]+)"
  }
  ["namespaces":protected]=>
  array(2) {
    ["middlewares"]=>
    string(0) ""
    ["controllers"]=>
    string(0) ""
  }
  ["paths":protected]=>
  array(2) {
    ["controllers"]=>
    string(11) "Controllers"
    ["middlewares"]=>
    string(11) "Middlewares"
  }
  ["mainMethod":protected]=>
  string(4) "main"
  ["cacheFile":protected]=>
  bool(false)
  ["cacheLoaded":protected]=>
  bool(false)
  ["errorCallback":protected]=>
  NULL
}

You can see demo included routes lists

from php-router.

izniburak avatar izniburak commented on August 16, 2024 1

@peter279k It make sense. Actually, I've started to work to add sub-folder support on php-router. I'm continue to work. We will figure it out.

from php-router.

izniburak avatar izniburak commented on August 16, 2024

Hi @tuyenlaptrinh ,
You should configure to base_folder as cms in Router default settings.
You can check out this guide: https://github.com/izniburak/php-router/wiki/2.-Getting-Started#other-settings

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

Hi @izniburak
I set base_folder as cms already.

I saw function run like this in your code.

public function run()
    {
        $documentRoot = realpath($_SERVER['DOCUMENT_ROOT']);
        $getCwd = realpath(getcwd());

        $base = str_replace('\\', '/', str_replace($documentRoot, '', $getCwd) . '/');
        $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$_SERVER['REQUEST_URI'] included cms folder like this

/cms/test?param=abc

from php-router.

izniburak avatar izniburak commented on August 16, 2024

I need to check the issue. I'm not sure what is the problem. I believe that i will fix it asap.
Thanks for feedback.

from php-router.

peter279k avatar peter279k commented on August 16, 2024

@tuyenlaptrinh, thanks for your reply.

If possible, could you provide completed sample codes?

I want to try to reproduce issue by myself. Thanks :).

from php-router.

peter279k avatar peter279k commented on August 16, 2024

After using your demo project, it's worked.

Here are my project structure on demo folder:

composer.json  composer.lock  index.php  vendor/

And the index.php:

<?php
require_once(__DIR__.'/vendor/autoload.php');
$router = new Buki\Router([
    'base_folder' => __DIR__.'/cms'
]);
$router->get('/',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->get('/test',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->run();

After requesting the /:

/var/www/html/demo/index.php:7:
object(Buki\Router)[3]
  protected 'baseFolder' => string '/var/www/html/demo/cms' (length=22)
  protected 'routes' => 
    array (size=2)
      0 => 
        array (size=7)
          'route' => string '/' (length=1)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[2]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null
      1 => 
        array (size=7)
          'route' => string '/test' (length=5)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[4]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null
  protected 'groups' => 
    array (size=0)
      empty
  protected 'patterns' => 
    array (size=12)
      '{a}' => string '([^/]+)' (length=7)
      '{d}' => string '(\d+)' (length=5)
      '{i}' => string '(\d+)' (length=5)
      '{s}' => string '(\w+)' (length=5)
      '{u}' => string '([\w\-_]+)' (length=10)
      '{*}' => string '(.*)' (length=4)
      ':id' => string '(\d+)' (length=5)
      ':number' => string '(\d+)' (length=5)
      ':any' => string '([^/]+)' (length=7)
      ':all' => string '(.*)' (length=4)
      ':string' => string '(\w+)' (length=5)
      ':slug' => string '([\w\-_]+)' (length=10)
  protected 'namespaces' => 
    array (size=2)
      'middlewares' => string '' (length=0)
      'controllers' => string '' (length=0)
  protected 'paths' => 
    array (size=2)
      'controllers' => string 'Controllers' (length=11)
      'middlewares' => string 'Middlewares' (length=11)
  protected 'mainMethod' => string 'main' (length=4)
  protected 'cacheFile' => boolean false
  protected 'cacheLoaded' => boolean false
  protected 'errorCallback' => null
Hello World!

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

Hi @peter279k
I think you are wrong in base_folder.

from php-router.

peter279k avatar peter279k commented on August 16, 2024

@tuyenlaptrinh, I just let the base_folder be the demo/cms.

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

As your config.
Correct base_folder only demo. And please run with sub-folder like this http://localhost/demo
Dont run with primary domain.

from php-router.

peter279k avatar peter279k commented on August 16, 2024

@tuyenlaptrinh, I change my base_folder, and the index.php code snippets are as follows:

I also let the Apache Document root is /path/to/htdocs/demo.

The requested URL will be like http://localhost.

<?php
require_once(__DIR__.'/vendor/autoload.php');
$router = new Buki\Router([
    'base_folder' => __DIR__
]);
$router->get('/',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->get('/test',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->run();

If you want to request the /cms/test route on http://localhost/cms/test, I think you should define the /cms/test route on index.php.

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

No no. You don't understand me.
I install php-router in sub-folder called demo
And run like this http://localhost/demo/test
demo is primary folder
test is router config
When I get router info then it return with sub-folder name. It should be only router config without sub-folder

from php-router.

peter279k avatar peter279k commented on August 16, 2024

If you install the php-router in sub folder, you should change your Apache Document root and the folder should be same as php-router installation folder.
And enable your rewrite module.

As far as I know, the php-router should not be worked on sub-folder at this moment.

It seems that the base_folder setting is not for this scenario you mention because this setting assumes that the php-router is worked on primary folder, not on sub-folder.

@izniburak, I think it should add some features are about setBasePath method to let the php-router can work on sub-folder.

Here is the useful tutorial about how to let Slim 4 framework work on sub-folder :).

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

@peter279k I know and I understand. Just notice to @izniburak for this script can run on sub-folder

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

I just fixed this issue with some custom code. It is working fine for primary domain and sub-folder too.

This is my change

private function addRoute($uri, $method, $callback, $settings)
    {
        .......
        $page = dirname($_SERVER['PHP_SELF']);
        // This is tuyenlaptrinh's change
        $getCWD = realpath(getcwd());
        $documentRoot = realpath($_SERVER['DOCUMENT_ROOT']);
        $base = str_replace('\\','/',str_replace($documentRoot,'',$getCWD));
        $page = str_replace($base,'',$page);
        // end tuyenlaptrinh change
        $page = $page === '/' ? '' : $page;
        if (strstr($page, 'index.php')) {
            $data = implode('/', explode('/', $page));
            $page = str_replace($data, '', $page);
        }
        ........
    }
public function run()
    {
        $documentRoot = realpath($_SERVER['DOCUMENT_ROOT']);
        $getCwd = realpath(getcwd());

        $base = str_replace('\\', '/', str_replace($documentRoot, '', $getCwd));
        $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// this is my change
        $uri = str_replace($base,'',$uri); 
        $uri = str_replace('/index.php','',$uri);
        $uri = str_replace('index.php','',$uri);
// this is my change
        if (($base !== $uri) && (substr($uri, -1) === '/')) {
            $uri = substr($uri, 0, (strlen($uri)-1));
        }

        if ($uri === '') {
            $uri = '/';
        }
        ..........
    }

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

I just found more issue.
We only can run http://localhost/demo
It will show not found error if running with http://localhost/demo/index.php

from php-router.

peter279k avatar peter279k commented on August 16, 2024

Actually, this router is not dispatched very well.

If having any issue, you should create another issue thread.

from php-router.

peter279k avatar peter279k commented on August 16, 2024

And according to the customized codes, it seems that this is a temporary fix.

Using some methods like setBasePath is proper.

from php-router.

izniburak avatar izniburak commented on August 16, 2024

Hi,
I've just tried this problem again on my computer (os: MacOS). Looks everything is okay.
I created a new directory that called test in localhost basepath (for example: /var/www/html). Then, I installed the Router package via composer require izniburak/router command. Now, my application path is /var/www/html/test. Then, I created index.php file and wrote required codes. (include autoload file and Router definitions as in the Router documentation.)

So, that's okay! I had no problem. Maybe, your problem may be related with Windows OS paths. But, i'm not sure. I will test same scenario on a Windows OS. I will give information ASAP.

Maybe you can re-check your configuration and paths. Looks somethings are wrong.

Thanks.

from php-router.

peter279k avatar peter279k commented on August 16, 2024

@izniburak, your setup will be fine, but it's not satisfied with @tuyenlaptrinh.

I assume that the index.php is:

<?php
require_once(__DIR__.'/vendor/autoload.php');
$router = new Buki\Router([
    'base_folder' => __DIR__
]);
$router->get('/',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->get('/test',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->run();

You can print the $router variable. And it will be following snippets:

/var/www/html/demo/index.php:7:
object(Buki\Router)[3]
  protected 'baseFolder' => string '/var/www/html/test' (length=18)
  protected 'routes' => 
    array (size=2)
      0 => 
        array (size=7)
          'route' => string '/test/' (length=6)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[2]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null
      1 => 
        array (size=7)
          'route' => string '/test/test' (length=10)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[4]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null
  protected 'groups' => 
    array (size=0)
......

But the expected should be:


/var/www/html/demo/index.php:7:
object(Buki\Router)[3]
  protected 'baseFolder' => string '/var/www/html/test' (length=18)
  protected 'routes' => 
    array (size=2)
      0 => 
        array (size=7)
          'route' => string '/' (length=6)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[2]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null
      1 => 
        array (size=7)
          'route' => string '/test' (length=10)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[4]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null
  protected 'groups' => 
    array (size=0)
......

from php-router.

izniburak avatar izniburak commented on August 16, 2024

You mean, Router is working correctly in base or sub-folder, but route paths seem like wrong? Route paths should not contain sub-directory name, right?

from php-router.

tuyenlaptrinh avatar tuyenlaptrinh commented on August 16, 2024

Just print $router and check each router then you can understand

$router->get('/test',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});

Print result

array (size=7)
          'route' => string '/test/test' (length=10)
          'method' => string 'GET' (length=3)
          'callback' => 
            object(Closure)[4]
              ...
          'name' => null
          'before' => null
          'after' => null
          'group' => null

This is error
'route' => string '/test/test' (length=10)

from php-router.

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.