Welcome Guest, Not a member yet? Register   Sign In
Bug? CI 4 not remove method from params when use routes ?
#1

(This post was last modified: 06-28-2020, 11:44 AM by cvlancvlan.)

Url: site[.]com/p/seo-url/param1/param2/...

app/Config/Routes.php 
PHP Code:
$routes->add('p/(:any)''Post::index'); 


app/Controllers/BaseController.php

PHP Code:
public function _remap($method, ...$params) {
    if ( 
method_exists($this$method) ) {
        return 
$this->{$method}(...$params);
    } elseif ( 
method_exists($this'index') ) {
        return 
$this->index(...$params);
    } throw \
CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();


In CI 3 it works perfect but in CI 4 i have some problems...

In CI3 $method from _remap is seo-url => the second element from uri_string
In CI4 $method from _remap is index => the method from routes

In CI3 $params from _remap is [param1, param2]
In CI4 $params from _remap is []

If i use this $routes->add('p/(:any)', 'Post::index/$1'); i recive this params [index, param1, param2] in _remap
The method index is not removed from params when use this $routes->add('p/(:any)', 'Post::index/$1');

How can i receive parameters without method in _remap when use routes ?

I think this is a bug in CI 4 because:
$routes->add('p/(:any)', 'Post::index');
This url ( site[.]com/p/seo-url/param1/param2/ ) will return this parameters [seo-url, param1, param2] => here the first element from parameters is the method
This url site[.]com/post/seo-url/param1/param2/ ) will return this parameters [param1, param2] => here the method is removed from parameters
Reply
#2

In CI 3 i use this _remap in MY_Controller and work perfectly for my app.
PHP Code:
public function _remap($method$params = array()) {
        if ( method_exists($this$method) ) {
            return call_user_func_array(array($this$method), $params);
        } elseif ( method_exists($this'index') ) {
            
$this->router->method 'index';
            
$this->router->uri->rsegments[2] = 'index';
            return 
call_user_func_array(array($this'index'), $params);
        } else {
            
show_404();
        }
    } 

For some days i try to find a good solution for this _remap in CI 4 for migrate my app to ci 4. 

Original url: site[.]com/post/seo-url/param1/param2

$routes->add('p(/.*)?', 'Post::index');
$routes->add('post(/.*)?', '404');

After route: site[.]com/p/seo-url/param1/param2
But in CI4 i received empty params after use this route in _remap
In CI3 i received params without method and folders in _remap

I need receive params without method and folders in CI 4 _remap.

Can someone help me please ? Thank you!
Reply
#3

(This post was last modified: 06-29-2020, 03:05 AM by cvlancvlan.)

This is my solution for CI 4. If someone have a good idea please tell me.

PHP Code:
public function _remap($method, ...$params) {
        
        
$router service('router');
        
        
$directory trim(strtolower((string) $router->directory()), '/');
        
$directory explode('/'$directory);
        
        
$segments = (array) $this->request->uri->getSegments();
        
        if ( !empty(
$directory) ) {
            
            
$segments array_udiff($segments$directory'strcasecmp');
            
        }
        
        
$segments array_values($segments);
        
array_unshift($segmentsNULL);
        unset(
$segments[0]);
        
        
$controller $segments[1] ?? '';
        
$method $segments[2] ?? '';
        
        
$params array_slice($segments2);
        
array_unshift($paramsNULL);
        unset(
$params[0]);
            
        if ( 
method_exists($this$method) ) {
            return 
$this->{$method}(...$params);
        } elseif ( method_exists($this'index') ) {
            return 
$this->index(...$params);
        } throw \
CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        
    } 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB