开发者

Are action parameters bad design/architecture?

This question can be applied to any programming language, but as I am thinking of PHP, I will phrase it accordingly...

I'm wondering if it is considered bad design/architecture if a web application uses action parameters, versus seperate files for each action.

For example: /index.php?action=edit

Versus

/edit.php or /index/edit.php

I know mod_rewrite can translate a 开发者_如何转开发pretty-url to a parametrized url, but I try to avoid uneeded complexity when not necessary.

Thanks.


Quite often, for big applications, (especially with Frameworks, such as symfony, Zend Framework, ...) we tend to use one entry point : index.php.

That entry point will receive some informations (like your action parameter), that will allow it to route the request to the correct controller (or any equivalent you might have).

So, to make things short : no, using action parameters is not bad design / architecture.
Of course, this depends on the kind of application -- but, generally speaking, have a unique entry-point is quite a good idea.


Well I don't think it is a bad design - of course there is other possibilities - but overall it is about your in-house agreements between the programmers how you do it. As much as you can you should split the PHP and HTML code to make the development easier further on.

I prefer the MVC-coding style, which splits the PHP and HTML from each other as much as you "want it to".

Hope this is helpful :)


I would call both your examples at least outdated or short of best practice.

/index.php?action=edit

Doesn't look good and is therefore not user friendly and isn't SE friendly either.

/edit.php

Means that there is indeed a single file for each action which clearly is bad practice in the 21st century where we have great MVC frameworks which enable us to get rid of this clutter and keep the concerns separated.

A good URL looks for example like that:

mysite.com/user/profile/edit

meaning where in the user module, the user-profile controller and the edit action.


There is nothing actually bad in either, both can be used all right

Separate files considered to be better for the small application, to avoid unnecessary complexity as you said.

Action way is considered better to serve complex applications featuring single entry-point working as a boot-strap, initializing all the site features first and then calling appropriate module.

I just have to warn you against using such action in silly way, doing include $_GET['action'] in the middle of main 'design' file. it's both insecure and unreliable.


It depends on your requirements and scale.

Using separate files is ok. However, you will probably find yourself copying code and not properly reusing code and techniques. It's easier to do this with small, ad hoc applications, but could very well turn into spaghetti code or a jungle nest over time.

If you use a single point of entry (class loading with url handlers), you do have to learn how that works (CodeIgniter and ExpressionEngine are good MVC systems to use if you're not real good at it yet), but it's more consistent in coding practice, and scales better than a bunch of separate pages or a switch() statement you pass everything through.

It's not a panacea, either way, but most professional operations use something like an entry point with a class loading system (such as MVC).


About using mod_rewrite: mod_rewrite was not created and should not be used as part of your PHP architecture.

About having one file per logical element. This is a very good and practical way of separating logical units in your app. It way better than creating huge files with a lot of mixed logic inside, which will become unmaintainable as the app grows. This has no contradiction on having one point of entry and an MVC architecture.

Having action parameters is the most normal approach for CRUD controllers for example where makes a lot of sense to group actions to a common controller

// blog controller
-> create blog entry 
-> edit
-> view
-> delete
-> list  ( this is a very common addition to CRUD

all this have a common architecture in the sense that almost all accept an id and do related thing.

If you are talking strictly about GET parameters than you'll see that creating medium/large application where you direct everything from a file and the only thing changing is the get parameters will outgrow you very fast. Computer architecture is like real architecture, try to split thing in small, simple (maybe reusable) units.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜