CakePHP4 Extract redirect route from url string

I want to redirect user after login.
For example, user access page /localhost/appfolder/Articles/view/1
User then redirected to login page. On the login page, it has GET parameter as ?redirect=/appfolder/Articles/view/1
I keep that parameter to redirect later, however, if passsing that string to $this->redirect($url_string) then the result url is
/appfolder/appfolder/Articles/view/1

How do i extract ['controller' => 'Article', 'action' => 'view', 1] from the strong /appfolder/Articles/view/1

is just a string builder. Assign that to another variable, and use str_replace() or similar to remove it from your $url_string. If that /1 is variable use preg_replace() with some fun regex (assuming its not in a loop, degrading performance).

Thank you Mr. Jawfin,

I figure out solution for my redirect after login issue in this post: https://discourse.cakephp.org/t/after-login-redirect-to-the-page-before-login-form/8577/12

Then i dont need to do extraction anymore. Thank again.

With another CakePHP 4 installation on Linux, I can redirect after login for only the first few days. After a weeks of develop other models and controller, the redirection does not work anymore. Did not change anything in authentication process apart from adding LDAP identifier.

What I did for Authentication are as below, the code is copied from my application, based on CakePHP 4 documentation.
In Application.php:
Implement the interface

class Application extends BaseApplication implements AuthenticationServiceProviderInterface

the use statements

use Cake\Core\Configure;
use Cake\Core\ContainerInterface;
use Cake\Core\Exception\MissingPluginException;
use Cake\Datasource\FactoryLocator;
use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\BaseApplication;
use Cake\Http\Middleware\BodyParserMiddleware;
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\ORM\Locator\TableLocator;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Identifier\IdentifierInterface;
use Authentication\Middleware\AuthenticationMiddleware;
use Cake\Routing\Router;
use Psr\Http\Message\ServerRequestInterface;

the middleware function

public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middlewareQueue
            ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
            ->add(new AssetMiddleware([
                'cacheTime' => Configure::read('Asset.cacheTime'),
            ]))
            ->add(new RoutingMiddleware($this))
            ->add(new BodyParserMiddleware())

            ->add(new AuthenticationMiddleware($this))
            
            ->add(new CsrfProtectionMiddleware([
                'httponly' => true,
            ]));
        return $middlewareQueue;
    }

The getAuthenticationService function

public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        $service = new AuthenticationService();

        // Define where users should be redirected to when they are not authenticated
        $service->setConfig([
            'unauthenticatedRedirect' => Router::url([
                    'prefix' => false,
                    'plugin' => null,
                    'controller' => 'Users',
                    'action' => 'login',
            ]),
            'queryParam' => 'redirect',
        ]);

        $fields = [
            IdentifierInterface::CREDENTIAL_USERNAME => 'username',
            IdentifierInterface::CREDENTIAL_PASSWORD => 'password'
        ];
        // Load the authenticators. Session should be first.
        $service->loadAuthenticator('Authentication.Session');
        $service->loadAuthenticator('Authentication.Form', [
            'fields' => $fields,
            'loginUrl' => Router::url([
                'prefix' => false,
                'plugin' => null,
                'controller' => 'Users',
                'action' => 'login',
            ]),
        ]);

       // Load identifiers
        $service->loadIdentifier('Authentication.Ldap', [            
            'host' => Configure::read('LDAP_server'),
            'port' => Configure::read('LDAP_port'),
            'bindDN' => function ($username) {
                return "$username" . Configure::read('LDAP_suffix');
            },
            
            'ldap' => \Authentication\Identifier\Ldap\ExtensionAdapter::class,
            'options' => [
                \LDAP_OPT_PROTOCOL_VERSION => 3,
            ],
        ]);
        
        //$service->loadIdentifier('Authentication.Password', compact('fields'));
        $service->loadIdentifier('Authentication.Password',
                                ['fields' => $fields,
                                 'resolver' => ['className' => 'Authentication.Orm', 'finder' => 'active']
                                ]
        );

        

        return $service;
    }

And login() function in UsersController is as normal login with little LDAP data extraction and formatting
The code for redirection is as below

            $target = $this->Authentication->getLoginRedirect() ?? '/timesheets';
            return $this->redirect($target);

However, the redirected route always is: /timesheets
What can I fixed?