dousi2553 2012-07-18 00:02 采纳率: 0%
浏览 81

如何在没有'胖'控制器的情况下实现干净的URL?

Recently i've been learning how to implement an MVC structure in my applications. I'm planning to use a PHP framework in the near future, but for now it's just pure PHP. I've learned that controllers should be 'skinny' and models should be 'fat', but can't seem to understand how this can go hand in hand with clean URLs.

At the company where I'm doing my internship I'm developing a web-based application to perform CRUD actions on 3 different tables in a database. Each of the tables has its own specific fields, which means that for each of those tables there are 4 views needed (1 for each CRUD action).

I want to make use of clean URL's, so right now i have 1 (only one) controller (index.php) which splits up the URL in segments and analyzes them, calling the specific methods and views for each segment, or (if needed) redirecting the user to a correct URL.

Right now the controller contains close to 1000 lines of code. I always read that controllers should be 'skinny', and see people talking about multiple controllers in one application. I can't seem to understand how to implement this in combination with clean URLs...

Any help would be much appreciated. At the company I'm doing my internship at there's just a web designer, so I can't ask him any programming-related questions...

Below is a basic representation of my controller (index.php)

// Check if user is logged in
if (isset($_SESSION['username']) AND isset($_SESSION['loggedIn'])) {

    // Get URL from $_SERVER
    $url = $_SERVER['REQUEST_URI'];

    // Split URL and assign values to $url
    $url = trim($url, "/");
    $url = explode('/', $url);

    // Remove first url segment (index.php) for convenience
    array_shift($url);

    // Check if 1st URL segment (category) is set
    if (isset($url[1])) {
        $category = $url[1];

        // Check if category is 'apples'
        if ($category == 'apples') {

            // Check if 2nd URL segment (action) is set
            if (isset($url[2])) {
                $action = $url[2];

                // Check if action is 'add'
                if ($action == 'add') {
                    // Calls to Model
                    // Include Presentation (form to add new record to 'apples' category)
                }

                // Check if action is 'view'
                elseif ($action == 'view') {
                    // Calls to Model
                    // Include Presentation (list of all records in the 'apples' table)
                }

                // Check if action is 'edit'
                elseif ($action == 'edit') {

                    // Check if 3d URL segment (id) is set
                    if (isset($url[3])) {
                        $id = $url[3];

                        // Calls to Model
                        // Include Presentation (form to edit record of the 'apples' category);
                    }

                    // If 3d URL segment (id) is not set then redirect
                    else {
                        header('Location: index.php/$category/view');
                    }
                }

                // Check if action is 'delete'
                elseif ($action == 'delete') {

                    // Check if 3d URL segment (id) is set
                    if (isset($url[3])) {
                        $id = $url[3];

                        // Calls to Model
                        // Include Presentation (form to edit record of the 'apples' category);
                    }

                    // If 3d URL segment (id) is not set then redirect
                    else {
                        header('Location: index.php/$category/view');
                    }
                }

                // If 2nd URL segment (action) is invalid then assume user wants to view and redirect
                else {
                    header("Location: index.php/$category/view");   
                }
            } 

            // If 2nd URL segment (category) is not set then assume user wants to view and redirect
            else {
                header("Location: index.php/$category/view");
            }

        }

        // Check if category is 'pineapples'
        elseif ($category == 'pineapples') {
            // same here as in 'apples' code block
        }

        // Check if category is 'pears'
        elseif ($category == 'pears') {
            // same here as in 'apples' code block
        }

        // If 1st URL segment (category) is invalid then redirect to index.php
        else {
            header('Location: index.php')
        }
    }

    // If 1st URL segment (category) is not set then show category overview
    else {
        include 'presentation/category_overview.php';
    }
}

// If user is not logged in then check if login form got submitted
elseif($_POST['formSubmit'] == 'submit') {
    // Calls to Model (form and user credentials validation)
    // Include Presentation (category overview)
}

// If user is not logged in and did not submit login form then include view (login form)
else {
    include 'presentation/login.php';
}
  • 写回答

2条回答 默认 最新

  • dougan1205 2012-07-18 00:35
    关注

    That's not how MVC is implemented!

    • First of all you must have a class for each controller, model or view.
    • Each class must be in a separate file.
    • Files must be grouped in separate directories according to their class type.

    consider you have an order table then you have:

    /index.php
    /controller/order.php
    /model/order.php
    /view/order/grid.php
    /view/order/form.php
    

    in index you must just route the program. you must instantiate the controller, call the demanded function. the controller must get the required object from model and put them in a view and return the view to index. index will call render on view and it will output the result.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效