duanlaiquan8174 2012-10-27 04:31
浏览 47
已采纳

php在自定义cms中动态创建新页面

I'm creating my own CMS from scratch as a way to build my php and mysql skills. Everything is going well, but I'm at the point where I want to create individual post pages for each blog post I write. So the index.php page has a list of all my blogs with snippets of each post and there is a read more button that should take the user to the full page for each blog post. Each post has a url created from the blog title entered in the "create post" form. I'm trying to figure out how to create unique pages for each post without passing the title, subhead, post content and other info through the GET.

This also dovetails with another feature I'm trying to add. I want to be able to create individual pages using a "create page" form like I did for my posts. So if I want an "about us" page I go to my admin form, fill out the title, add the content, and when I hit submit it creates the page dynamically. I have thought all day about how I'd do these two things but can't quite figure out how I can do this.

FYI, I'm not asking for code, I just need a push in the right direction as I try to conceptualize how to achieve this. Thanks!

  • 写回答

3条回答 默认 最新

  • duanniedang3946 2012-10-27 04:50
    关注

    If you're not familiar with the Model-View-Controller pattern, reading up on it might be prudent. MVC is frequently the right starting place for high-level design of web applications.

    Also, a CMS is a big enough project you should consider using a PHP framework like CodeIgniter, Symfony, Zend, etc. to make your life easier. It removes a lot of the drudge work and common tasks.

    Dynamic Page Creation and Display

    I think you want to split it into two things: the text content (basically what you put in the forms) and the HTML templating surrounding that content.

    When you make a page or blog post, you would want to store the actual content (what you type into the creation form) in a database of some sort (not necessarily an RDBMS, but if you're trying to build MySQL skills it's a reasonable choice).

    Then you would use a separate function to bind that content into an HTML template and present it to the user when they load a given page.

    URL Routing

    To get nicer-looking URLs you can use something like apache's mod_rewrite. You can use that to convert a URL like this:

    posts/how-to-make-a-cms

    to this:

    posts.php?title=how-to-make-a-cms

    Then you can have posts.php read from GET as normal. How you choose to do the conversion is pretty open-ended.

    To avoid getting really complicated rewrites, people often just structure everything to go to a central routing script which figures out what class and method to call and what arguments to pass it. So it would rewrite the URL above to:

    main.php?a=posts/how-to-make-a-cms

    Then main.php would parse out the segments of that argument from GET and figure out where to send them. Like it might take posts/show/how-to-make-a-cms and do something like:

    $o = new Posts();
    $o->show("how-to-make-a-cms");
    

    If you do it that way, I think you can avoid mod_rewrite entirely as long as you're willing to accept only slightly pretty URLs, like this:

    mysite.com/main.php?/posts/show/how-to-make-a-cms

    I haven't done this type of thing before (because the frameworks do it so beautifully already), so I might be missing some minor details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?