• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirection issues

#1
Good afternoon friends, can you help me?
In other controllers I am able to work perfectly with CI4's native redirect () method, however in this method below it does not return any error and also does not redirect to the last route.



PHP Code:
public function isAllowed()
    {
       if(!empty(Utils::getDataSession('username'))) :

        $isAllowed false;

        $routeName Utils::getDataMachine('REQUEST_URI');
        $getPermission Utils::getDataSession('permission');
        
            
if (in_array($routeName$this->exception)) :
                $isAllowed true;
            else :

                foreach ($getPermission as $key => $value) :

                    if ($value == $routeName) :
                        $isAllowed true;
                    endif;

                endforeach;

                $permission = new Permission;

                if (!$isAllowed) :
                    return redirect()->route('dashboard');
                endif;

            endif;

        else :
            return redirect()->route('/');
        endif;
    
Reply

#2
Leaving only the return redirect is still not working.




PHP Code:
public function isAllowed()
    {   
        
return redirect()->route('/');
    
Reply

#3
Can you paste your entire controller?
Website: marcomonteiro.net  | Blog: blog.marcomonteiro.net | Twitter: @marcogmonteiro | TILThings: tilthings.com
Reply

#4
Are you using the production version or the developer version of CodeIgniter 4?

There was a problem with the developer version until they fix it, with the .htaccess file.

You need to change this:

Code:
RewriteRule ^(\s\S*)$ index.php/$1 [L,NC,QSA]

To this:

Code:
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

That is only for the latest developer version of CodeIgniter 4.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(07-10-2020, 01:24 AM)marcogmonteiro Wrote: Can you paste your entire controller?
Hi friend, thanks for replying.


Permission.php


PHP Code:
<?php

/**
 * @package Permission Controller
 * @author Wickfield <Boris#8779>
 **/

namespace App\Controllers;

use 
App\Models\PermissionModel;

use 
App\Helpers\Utils;

class 
Permission extends BaseController
{
    private $exception = ["/""/authenticate""/logout"];

    public function __construct()
    {
        $this->model = new PermissionModel;
    }

    public function getMenu()
    {
        return $this->model->constructMenu();
    }

    public function getSubMenu(string $id)
    {
        return $this->model->getSubMenu($id);
    }

    public function getModules(string $code)
    {
        return $this->model->getModules($code);
    }

    public function getAllowedRoutes(string $username)
    {
        $permissions = [];

        $data $this->model->getAllowedRoutes($username)->getResult();

        if ($data) :

            foreach ($data as $key => $value) :
                array_push($permissions$value->route);
            endforeach;

        endif;

        return $permissions;
    }

    public function isAllowed()
    {
       if(!empty(Utils::getDataSession('username'))) :

        $isAllowed false;

        $routeName Utils::getDataMachine('REQUEST_URI');
        $getPermission Utils::getDataSession('permission');
        
            
if (in_array($routeName$this->exception)) :
                $isAllowed true;
            else :

                foreach ($getPermission as $key => $value) :

                    if ($value == $routeName) :
                        $isAllowed true;
                    endif;

                endforeach;

                $permission = new Permission;

                if (!$isAllowed) :
                    return redirect()->route('dashboard');
                endif;

            endif;

        return $isAllowed;

        else :
            return redirect()->route('/');
        endif;
    }




BaseController:

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;

use 
App\Libraries\Template;
use 
App\Helpers\Ajax;
use 
App\Controllers\Permission;

class 
BaseController extends Controller
{
    protected 
$helpers = [];
    protected 
$template;
    protected 
$ajax;
    protected 
$permission;

    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        parent::initController($request$response$logger);

        
$this->template = new Template;
        
$this->ajax = new Ajax;
        
$this->permission = new Permission;
    }



Since the Permission class is in autoload, I only call $this->permission->isAllowed() in the other methods.
Reply

#6
(07-10-2020, 06:37 AM)InsiteFX Wrote: Are you using the production version or the developer version of CodeIgniter 4?

There was a problem with the developer version until they fix it, with the .htaccess file.

You need to change this:

Code:
RewriteRule ^(\s\S*)$ index.php/$1 [L,NC,QSA]

To this:

Code:
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

That is only for the latest developer version of CodeIgniter 4.


Hi friend, thanks for replying.
I am using the production version of CI4.
Reply

#7
Is that method in a controller?

I always use an echo and exit to step through the code to find errors.


PHP Code:
echo $whatever;
exit(); 

I start at the top and work down checking each string and array values to see if there correct.

Also I would check and make sure that your getting the request class in there.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
(07-10-2020, 11:51 AM)InsiteFX Wrote: Is that method in a controller?

I always use an echo and exit to step through the code to find errors.


PHP Code:
echo $whatever;
exit(); 

I start at the top and work down checking each string and array values to see if there correct.

Also I would check and make sure that your getting the request class in there.

Yes, that method is on a controller.

So friend, if I put the echoes it respects the condition structure and prints accordingly.


PHP Code:
public function isAllowed()
    {
       if(!empty(Utils::getDataSession('username'))) :

        $isAllowed false;

        $routeName Utils::getDataMachine('REQUEST_URI');
        $getPermission Utils::getDataSession('permission');
        
            
if (in_array($routeName$this->exception)) :
                $isAllowed true;
            else :

                foreach ($getPermission as $key => $value) :

                    if ($value == $routeName) :
                        $isAllowed true;
                    endif;

                endforeach;

                $permission = new Permission;

                if (!$isAllowed) :
                    //return redirect()->route('dashboard');
                    echo "false";
                endif;

            endif;

        return $isAllowed;

        else :
            echo "else";
            //return redirect()->route('/');
        endif;
    

If I put it to redirect with javascript it works normally

PHP Code:
public function isAllowed()
    {
       if(!empty(Utils::getDataSession('username'))) :

        $isAllowed false;

        $routeName Utils::getDataMachine('REQUEST_URI');
        $getPermission Utils::getDataSession('permission');
        
            
if (in_array($routeName$this->exception)) :
                $isAllowed true;
            else :

                foreach ($getPermission as $key => $value) :

                    if ($value == $routeName) :
                        $isAllowed true;
                    endif;

                endforeach;

                $permission = new Permission;

                if (!$isAllowed) :
                    echo "<script>window.location.href='dashboard'</script>";
                endif;

            endif;

        return $isAllowed;

        else :
            echo "<script>window.location.href='/'</script>";
        endif;
    
Reply

#9
Are you using the 4.0.3 production build of CodeIgniter or the developer version?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
(07-11-2020, 03:16 AM)InsiteFX Wrote: Are you using the 4.0.3 production build of CodeIgniter or the developer version?
Hello, I'm using production version 4.0.3
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.