How to format url formatting MVC form?

I am using PHP. I want to create an MVC setup from scratch to learn more about how MVC works. I want to use pure slash URLs as delimiters for arguments. How do people do this when it comes to forms of the GET method? Or do people avoid getting the GET method together?

As of now, I can imagine:

  • Do not use forms of the GET method (although in some cases this makes access to the bookmark / link more difficult).
  • Use AJAX instead of submitting the form (although what do you do for SEO and JS disablers?).
  • Imagine a page with an email method, then reformat post vars to a URL, then redirect to that URL using headers (looks like wasted resources).

Any suggestions or recommended readings are welcome.

+3
source share
10 answers

Get variables and clean URLs do not contradict each other. You can always have urls like

http://example.com/controller/action?value1=foo&value2=bar

Alternate URL style may also look like

http://example.com/controller/action/value1/foo/value2/bar or http://example.com/controller/action/foo/bar

In these two cases, if you want to create these URLs via the GET submit form, you will need to use JavaScript to build the correct URL, so the very first solution may be easier to implement.

Another question is the solution between the presentation of POST and GET. POST is more secure, but as you said, users cannot add it.

+3

.htaccess, . < IfModule mod_rewrite.c >
          RewriteEngine On
            RewriteCond% {REQUEST_FILENAME}! -d
            RewriteCond% {REQUEST_FILENAME}! -f
        RewriteRule ^ (. *) $index.php? url = $1 [QSA, L]
</IfModule>

... URL- http://example.com/controller/action/param1/
, index.php var url [string]
... ,

$params = explode('/', $_GET['url']);
$controller = new $params[0];//load the controller
$controller->$params[1]($params[2]);//ejecute the method, and pass the param
+6

, 3 , MVC:

+3

, URL-, MVC. REST. ( )

URL , .

:

Apache mod_rewrite PATH GET. , , :

http://your.domain/users.php?request=edit&id=<some id>

Apache:

RewriteRule ^users\/edit\/(\d+) /users.php?request=edit&id=$1

http://your.domain/users/edit/<some id>

(, ID - )

, , , . , . , , , , Apache .htaccess. .

, URL. script . URL- :

http://your.domain/users.php/edit/<some_id>

<?php

// get the part that appears after the script name
$parts = explode("/", $_SERVER['PATH_INFO'] );

$request = $parts[1];
$id = $parts[2];

// do somethign with the $request and the $id 

?>

, , 2, , .

, .

+2

, - / MVC, , (, CodeIgniter), /​​ , .

, MOD_REWRITE, URL. GET - URL.

+1

Cfreak digg PHP, , MVC. PHP, , GET POST. , .

URL- PHP MVC : example.com/index.php/controller/action/arguments. index.php .htaccess. - , , , - . /, , next. , , , URL. , . index.php, , :)

+1

, . , URL-. .

, GET. , , URL-, :

http://www.example.com/search?name=lonesomeday&site=stackoverflow

, , :

http://www.example.com/search/lonesomeday

YMMV, .

0

- .

  • QueryString (...?a=b&c=d) "GET-", URL- .
  • , URL-, (GET/POST - ) :

    if (/* form has been submitted */) {
        $data = array_map("urlencode", $_GET);
        $url = sprintf('/%s/%s/%s', $data['a'], $data['b'], $data['c']);
    
        header("Location: $url");
        echo "Redirection: <a href='$url'>$url</a>";
        exit;
    }
    
  • , , JavaScript :

    formReference.addEventListener("submit", function() {
        /* do exactly the same as above PHP code */
    
        return false;
    }, false);
    

    , , - JS.

0

, , , SEO- ( ) URL- "www.site.com/controller/action/params". .htaccess , -, MOD_REWRITE. . , URL-.

0

, dqhendricks, , , - MVC, .

, GET . POST PUT. POST -. GET , , (, , ..).

:

1) GET

. , .

2) AJAX

. URL JS, .

3) post,, post vars URL-, URL-, (, ).

, . . . - POST, , . , ( ). , URL.

, URL- URL-, Front- . . , (www mobile), WWW .

In addition, IMO, you could say that the wasted resources will be APP, deciding where to redirect (or pass the redirect URL to an HTTP request or PHP session). In any case, the implementation of alternative methods requires development time and server resources.

So, in my opinion, path number 3 is the path.

0
source

Source: https://habr.com/ru/post/1784118/


All Articles