Home > database >  PDOCrud Routing libraries don't work together due to htaccess, why?
PDOCrud Routing libraries don't work together due to htaccess, why?

Time:01-04

I am using this enter image description here

After investigating around, I found that the issue was with the .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js)$
RewriteRule (.*) routes.php [QSA,L]

# BEGIN Pdocrud
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php 
</IfModule>

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# HTML
ExpiresByType text/html "access plus 2 days"
</IfModule>

# END Pdocrud

The first 3 lines are responsible for the routing. When I remove them PDOCrud works, but then my url will be localhost/public/view/reports.view.php instead of localhost/cases-reports.

How can I reconstruct the .htaccess to make it work?

PDOCrudAjaxCtrl.php

<?php

@session_start();

Class PDOCrudAjaxCtrl {

    public function handleRequest() {
        $instanceKey = $_REQUEST["pdocrud_instance"];
        if(!isset($_SESSION["pdocrud_sess"][$instanceKey])){
            die("Session has been expired. Please refresh your page to continue.");
        }

        $pdocrud = unserialize($_SESSION["pdocrud_sess"][$instanceKey]);
        $action = $_POST["pdocrud_data"]["action"];
        $data = $_POST["pdocrud_data"];
        $post = $_POST;
        if (isset($_FILES))
            $post = array_merge($_FILES, $post);
        $data["post"] = $post;
        switch (strtoupper($action)) {
            case "VIEW":
                echo $pdocrud->render("VIEWFORM", $data);
                break;
            case "SORT":
                $pdocrud->setBackOperation();
                $data["action"] = "asc";
                echo $pdocrud->render("CRUD", $data);
                break;
            case "ASC":
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "DESC":
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "ADD":
                echo $pdocrud->render("INSERTFORM", $data);
                break;
            case "INSERT":
                $pdocrud->render("INSERT", $post);
                break;
            case "INSERT_CLOSE":
                $pdocrud->setBackOperation();
                $pdocrud->render("INSERT", $post);
                break;
            case "INSERT_BACK":
                $pdocrud->setBackOperation();
                $pdocrud->render("INSERT", $post);
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "BACK":
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "EDIT":
                $pdocrud->setInlineEdit(false);
                echo $pdocrud->render("EDITFORM", $data);
                break;
            case "INLINE_EDIT":
                $pdocrud->setBackOperation();
                $pdocrud->setInlineEdit(true);
                echo $pdocrud->render("EDITFORM", $data);
                break;
            case "ONEPAGEEDIT":
                $pdocrud->setBackOperation();
                $pdocrud->setInlineEdit(false);
                echo $pdocrud->render("ONEPAGE", $data);
                break;
            case "ONEPAGEVIEW":
                $pdocrud->setBackOperation();
                echo $pdocrud->render("ONEPAGE", $data);
                break;
            case "QUICK_VIEW":
                echo $pdocrud->render("QUICKVIEW", $data);
                break;
            case "RELATED_TABLE":
                echo $pdocrud->render("RELATED_TABLE", $data);
                break;
            case "INLINE_BACK":
                $pdocrud->render("UPDATE", $post);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "UPDATE":
                $pdocrud->render("UPDATE", $post);
                break;
            case "UPDATE_BACK":
                $pdocrud->setBackOperation();
                $pdocrud->render("UPDATE", $post);
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "UPDATE_CLOSE":
                $pdocrud->setBackOperation();
                $pdocrud->render("UPDATE", $post);
                break;
            case "DELETE":
                $pdocrud->render("DELETE", $data);
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "DELETE_SELECTED":
                $pdocrud->render("DELETE_SELECTED", $data);
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "PAGINATION":
                $pdocrud->setBackOperation();
                $pdocrud->currentPage($data["page"]);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "RECORDS_PER_PAGE":
                $pdocrud->currentPage(1);
                $pdocrud->recordsPerPage($data["records"]);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "SEARCH":
                $pdocrud->currentPage(1);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "AUTOSUGGEST":
                if(isset($_GET["callback"]))
                    $data["callback"] = $_GET["callback"];
                echo $pdocrud->render("AUTOSUGGEST", $data);
                break;
            case "EXPORTTABLE":
                echo $pdocrud->render("EXPORTTABLE", $data);
                break;
            case "EXPORTFORM":
                $pdocrud->render("EXPORTFORM", $data);
                break;
            case "SWITCH":
                $pdocrud->setBackOperation();
                $pdocrud->render("SWITCH", $data);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "BTNSWITCH":
                $pdocrud->setBackOperation();
                $pdocrud->render("BTNSWITCH", $data);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "LOADDEPENDENT":
                echo $pdocrud->render("LOADDEPENDENT", $data);
                break;
            case "EMAIL" : echo $pdocrud->render("EMAIL", $post);
                break;
            case "SELECT":
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "SELECTFORM":
                echo $pdocrud->render("SELECT", $post);
                break;
            case "FILTER":
                $pdocrud->setBackOperation();
                $pdocrud->currentPage(1);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "RELOAD":
                $pdocrud->setBackOperation();
                echo $pdocrud->render("CRUD", $data);
                break;
            case "SAVE_CRUD_TABLE_DATA":
                $pdocrud->setBackOperation();
                $pdocrud->render("SAVE_CRUD_DATA", $data);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "RENDER_ADV_SEARCH":
                $pdocrud->currentPage(1);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "DATE_RANGE_REPORT":
                $pdocrud->setBackOperation();
                $pdocrud->currentPage(1);
                echo $pdocrud->render("CRUD", $data);
                break;
            case "CLONE":
                echo $pdocrud->render("CLONEFORM", $data);
                break;
            case "CELL_UPDATE":
                $updateData[$data["column"]] = $data["content"];
                 if (isset($data["id"]))
                    $pdocrud->setPrimarykeyValue($data["id"]);
                $pdocrud->render("UPDATE", $updateData);
                break;
            case "AJAX_ACTION":
                echo $pdocrud->render("AJAX_ACTION", $data);
                break;   
            case "PRINTPDF":
                echo $pdocrud->render("PRINT_PDF", $data);
                break;    
            default:
                break;
        }
    }
}

CodePudding user response:

RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js)$
RewriteRule (.*) routes.php [QSA,L]

# BEGIN Pdocrud
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php 
</IfModule>

The first rule rewrites everything to routes.php so the second rule is not even processed. You need to reverse these two rules for this to make any sense. However, it should be noted that the presence of both these systems would appear to impose a "silent" restriction on the type of URLs you can implement as "routes".

However, the second rule is not strictly correct anyway since it is potentially rewriting to a different URL than the one being tested in the preceding condition (which could potentially result in a rewrite-loop, ie. a 500 error, for certain requests. See the following question on ServerFault for more detail on this).

Try the following instead (assuming the .htaccess file is in the document root):

RewriteEngine On

# BEGIN Pdocrud
RewriteCond %{REQUEST_URI} !\.(php|png|jpe?g|gif|zip|css|svg|js)$
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# HTML
ExpiresByType text/html "access plus 2 days"
</IfModule>
# END Pdocrud

RewriteRule !\.(png|jpg|gif|jpeg|zip|css|svg|js)$ routes.php [L]

Additional notes:

  • In the "Pdocrud" rule that appends the .php extension, there is no point checking that the request does not map to a directory only to then check that it does map to a file.

  • In your "routing" rule, the preceding condition was not required - this check should be performed in the RewriteRule instead. Likewise, the QSA flag was not required.

  • A restriction with this config is you cannot have a "route" that also happens to map to a .php file (when .php is appended).

  •  Tags:  
  • Related