构架上的困惑
大家好
小弟曾经写过一个网站,用的方式如下
index.php 首页
后台logic 有一个php文件,logic.php
在 logic.php 里面写了
$action=_$POST[action]
switch $action
case check_userlogin
login(_$POST[username],_$POST[pwd]);
case put_comment
put_comment(_$POST[Comment]);
...
end switch
通过这样的方式调用不一样的action 执行不同的函数, logic.php就像所有操作的通用入口。
因为最近看了mvc框架,虽然明白原理,但是不深入的话感觉还是有些困难。
如果自己做一个小项目,这样switch的结构合适不合适呢?
前台如果用ajax,一般我就会调用 url:logic.php
data:{action:update_commnet,commnet:"hellow"} 类似这种
因为时间比较紧张,如果再现学一个MVC我怕会来不及,请给点建议
谢谢各位
回复讨论(解决方案)
没人回复,难道是我提问的方式不对?。。
你的那个写法与mvc的路由是一样的
不同之处在于你处理的是有限(已知)方法,而mvc路由可处理无限方法(增加方法不需更改路由代码)
没人回复,难道是我提问的方式不对?。。
这种方式可以啊,我也是用这种模式的
楼主对这种方式有什么质疑吗?
action少的话,没有问题,但是action过多的话,建议做成配置型的选择。
例如:
$actions = array(
'check_userlogin' => 'login',
.....
);
$action=_$POST[action];
if(isset($actions[$action]) && function_exists($actions[$action])) {
$actions[$action]();
}
你的那个写法与mvc的路由是一样的
不同之处在于你处理的是有限(已知)方法,而mvc路由可处理无限方法(增加方法不需更改路由代码)
原来如此,难怪我要定义很多的action每次都需要case多一个,真是一语道破天机,谢谢了,不过小项目路由不多的话应该问题不大吧。
而且我还把自己的template框架写在后台一个template文件中EOT里面,没有用模板引擎
action少的话,没有问题,但是action过多的话,建议做成配置型的选择。
例如:
$actions = array(
'check_userlogin' => 'login',
.....
);
$action=_$POST[action];
if(isset($actions[$action]) && function_exists($actions[$action])) {
$actions[$action]();
}
嗯,很有意思,不过小项目action应该不会太多,不过这样好像是很好,action就对应每一个function名字就好了,多谢指点
因为对hatacees不是很熟悉,看了CI和YII对调用来调用去感觉有点乱,所以才有了这个问题
同意楼上几位的解答。
自从rails成功之后,所有框架都遵循一个理念:约定大于配置。
尽量用约定好的规则,来代替主动的配置。这样会让代码的可扩展性更强也会进一步减少后续的维护工作量。
我用的是if elseif

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov
