【问题标题】:Routing independant controllers and models in fat free framework在无脂肪框架中路由独立的控制器和模型
【发布时间】:2017-07-17 20:36:46
【问题描述】:

我正在尝试在 Fat Free 环境中创建主站点导航、侧边栏导航和页脚导航。我刚刚开始使用框架,尤其是 MVC 类型。

我的问题,因为我的导航几乎会出现在网站的每个页面上,我正在考虑创建单独的控制器和模型来处理所有这些人员,但不确定如果不进行路由它会如何工作?

另外,我不知道如何处理模型中的连接,我在网上根本找不到任何相关信息。

这是我当前的类别控制器

class Categories extends DB\SQL\Mapper
{
    public function __construct(DB\SQL $db)
    {
        parent::__construct($db, 'categories');
    }

    public function all()
    {
        $this->load();
        return $this->query;
    }

    public function getByID($id)
    {
        $this->load(array('id=?', $id));
        return $this->query;
    }

    public function getBySlug($category_slug)
    {
        $this->load(array('category_slug=?', $category_slug));
        return $this->query;
    }

    public function add()
    {
        $this->copyfrom('POST');
        $this->save();
    }

    public function edit($id)
    {
        $this->load(array('id=?', $id));
        $this->copyfrom('POST');
        $this->update();
    }

    public function delete($id)
    {
        $this->load(array('id=?', $id));
        $this->erase();
    }
}

任何想法或指示都会帮助我走很长的路。

提前致谢

【问题讨论】:

  • 这看起来像一个模型。不是控制器。
  • 这是一个模型,我的错

标签: php model-view-controller navigation fat-free-framework


【解决方案1】:

我不知道是否理解您的问题,但如果您想让控制器中的所有方法都可以使用类别,您可以使用beforeRoute() 方法:

class TestController extends MainController {

    // runs before routing
    // if another controller extends TestController and also has a 
    // beforeRoute, this will be overriden
    function beforeRoute() {

        // Load Categories
        $categories = new Categories($this->db);
        // Assiging ->all() to variable makes the method return an array of all results
        $categoriesArray = $categories->all();
        // Set an array in the hive for template use 
        $this->f3->set('categories', $categoriesArray );
        // Clear the instance
        $categories->reset();

    }

    function renderPage() {

        // the 'categories' hive variable is available because beforeRoute has been run 


        // Set the page title from the dictionary file
        $this->f3->set('pageTitle', $this->f3->get('DICT_'.'page_whatever') );
        // Render the View
        $this->f3->set('view','page.whatever.htm');
        $template=\Template::instance();
        echo $template->render('layout.sidebar.htm');

    }

// End of Controller
}

当然,在模板中:

<repeat group="@categories" value="@category">
  <li>
    <a href="{{ @category.uri }}">{{ @category.label }}</a>
  </li>
</repeat>

ps:您可能使用的是$f3 而不是$this-&gt;f3$db 也是如此)

【讨论】:

    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多