0

特定の市場向けに独自のカスタム CMS システムを開発することを検討しており、いくつかのフレームワークを検討してきましたが、それらの問題は、ルーティングが自動化されていないことです。laravelでは、このようなURLに正しく応答することを覚えていれば、次のようにするでしょう:

Route::get('/user', function()
{
    return "This is a user";
});

これは基本的に特定のリクエストをリッスンします。これを単純化するための私のアイデアは、自動ルーターを作成することでした。だから私がしたことは、すべてのリクエストを受け取り、それをindex.phpに向ける.htaccessファイルをセットアップすることでした。.com の後に何かが必要だったのでwww.testsite.com/admin/pages/edit/5、get 変数として追加しました。

上記の例では、1 つのリクエストで 4 つのパラメータを渡しています。

admin   -   request path / used to signify a login check must be done before passing 
            them on to their request
pages   -   This would be the class or object
edit    -   This would be the method called from the class / object
5       -   This would be the actual row of the record in the database being edited

そこで、次のようなルータークラスを開発しました。

class router {

     public $url;
     public $protectedPaths = array('admin','users','client');

     public function __construct() {
          $this -> url = explode('/', $_GET['url']);

          if($this -> url[0] == '') {
               $this -> loadDefaultView();
          } else {
               // Check to ensure that the path is not protected
               if(in_array($this -> url[0], $this -> protectedPaths)) {

                    // check to ensure user is logged in
                    if($_COOKIE['isLogged']) {

                         // This means that there is no action or model needed just a returned view
                         if($this -> url[2] == '') {

                              $this -> loadViewWithoutAction();

                         } else {
                              // we check to ensure there is a controller
                              if(file_exists(baseControllers .'controller.'. $this -> url[1] .'.php')) {

                                   // require that controller and instantiate it
                                   require baseControllers .'controller.'. $this -> url[1] .'.php';
                                   $obj = new $this -> url[1];

                                   // check to see if method exists
                                   if(method_exists($obj, $this -> url[2])) {

                                        if($_POST) {
                                             $data = $_POST;
                                        } else {
                                             $data = array($this -> url[3]);
                                        }

                                        // run method if necessary
                                        $data = call_user_func_array(array($obj, $this -> url[2]), $data);
                                        $this -> loadAdminView( $data );

                                   } else {
                                        $this -> loadErrorView();
                                   }

                              } else {
                                   $this -> loadErrorView();
                              }
                         }
                    } else {
                         header("Location: /auth/form");
                    }

               } else {

                    // we check to ensure there is a controller
                    if(file_exists(baseControllers .'controller.'. $this -> url[0] .'.php')) {

                         // require that controller and instantiate it
                         require baseControllers .'controller.'. $this -> url[0] .'.php';
                         $obj = new $this -> url[0];

                         // check to see if method exists
                         if(method_exists($obj, $this -> url[1])) {

                              // run method if necessary
                              $data = call_user_func_array(array($obj, $this -> url[1]), array($this -> url[2]));
                              $this -> loadPublicView( $data );


                         } else {
                              $this -> loadErrorView();
                         }

                    } else {
                         $this -> loadErrorView();
                    }

               }

          }
     }

したがって、さまざまな if else ステートメントと、おそらくスイッチを使用して、さまざまなリクエストなどを区別します。最後に私の質問は、クラスを自動ロードしてメソッドを実行するという悪い習慣です。フレームワークで見たものからすると、これはすべて手動であり、私は専門家ではないので、おそらくその理由があると思います。さらに、Web 上の OOP での PHP リクエストの自動化については、ほとんどわかりませんでした。

これを自動化したいのですが、同時にセキュリティ上の懸念も引き起こしたくありません。ああ、ユーザーが入力するフォームや個人情報については、URL のハッキングから保護するために、すべてが投稿または ajax になります。

事前にアドバイスや回答をありがとう!

4

0 に答える 0