doulu4976 2019-08-07 10:30
浏览 242

如何将动态URL值映射到方法

I know I come up with the worst questions. As I'm not great at coding. I am very confused about how Routing Works. I see a lot of MVC Tutorials, talking about first parameter being a controller and second a valid method and the rest being params, but it does not make sense to me, how would that work with most pages being dynamic? ideally I would prefer to avoid the MVC pattern and have each document responsible for itself.

But to keep it simple, Say I want the following clean dynamic URL:

/Products/fruits/oranges/bloodoranges

Not like this:

/Products/product/fruits or /Products/product?=fruits

Products can have: fruits, nuts, beans etc, and perhaps n number of products in the future.

Fruits can have: oranges, bananas, kiwi etc...

If the user clicks nuts, it should be

/Products/nuts

The next page is a sub category of one of these, and the next a further sub category.

What would be the best logic?

If I keep methods for fruits, nuts, etc within Products, then that seems inefficient to me

I have already redirected everything to index.php.

Basically I want each URL to map to a dynamic document which I can make up with other components like headers, footers etc, Rather than everything as Models, views, controllers

Can this work?

So far I have done the basics:

#HTAccess:
RewriteEngine on
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,NC,L]


//Request Class Constructor
function __construct()
{
foreach ($_SERVER as $key => $value) 
{$this->httpRequest[filter_var($key,
FILTER_SANITIZE_URL)] = filter_var($value, FILTER_SANITIZE_URL);
}

if (isset($_GET['url'])) {
$this->params = explode('/',    rtrim($_GET['url'], '/'));
$this->module = $this->params[0];
echo "Module: ". $this->module . "<br>";
unset($this->params[0]);
var_dump($this->params);
}

}

//Route
public function route()
{
if 
(file_exists(MODULES.'/'.$this->module . '/'.$this->module.'.php')).    {
        echo "File found";

    }else{
        echo "File not found";
    }
}

I was thinking of having a class with methods in an array like so:

class Module {
    private $route = ['product', 'category', 'subcategory'];
    //Map each URL to a method
    public function product($data){

       $this->createWiew(View $view, $data)
    }
}

I know it should be really simple, I am just not getting it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏