Home  >  Article  >  Backend Development  >  In-depth study of Tasks and views in Joomla

In-depth study of Tasks and views in Joomla

WBOY
WBOYOriginal
2016-08-08 09:23:391060browse

[This article is reproduced from: Mengxi Notes]

Joomla is an excellent CMS system that allows you to quickly complete the construction of a website. It provides components, modules, and templates to meet most of your website needs. Components play an important role in this.

1. Basic knowledge

Component (component) is used to display the main data of the page. Joomla's components are designed using the MVC architecture. When a page request is generated, its URL may include task, view, layout and other information. I am here to discuss this task and view. Generally, if the URL contains task, it will not contain view. This is because Joomla believes that task completes a specific task, such as database operation, validity verification, etc., and view is responsible for display. data. The usual design is that after completing the task processing in the task, the setRedirect method will be called to guide it to a view to display the data. In fact, in Joomla, if the task is not specified in the URL, the default task is display.

2. Question

In the project, the Open graphic protocol data needs to be included in the meta data of the page. Open graphic protocol is used to provide social networks with descriptions of data to be shared. If your page is completed through a task, and then you use setRedirect in the task to jump to different views based on the data for authorization verification, and then display the data page after the verification is passed, you may encounter this problem: You need to share For this page, you added Open graphic protocol data to the meta data of this page. When you want to share it to social networking sites such as facebook, google+, etc., you will find that the data and pictures displayed on the shared page are not the data you want to display on the page. .

3. Solution

The above problem is because the Open graphic protocol data acquisition does not support jumps. If you encounter a jump, you will usually go to the homepage of the website to pick up the data, which is not what we want. The problem is setRedirect. The principle of setRedirect is that the HTML header sent to the browser contains jump instructions. The way to solve the above problem is not to use setRedirect, but to use display. Each JControllerLegacy has a display method. You only need to set the view, layout, and other data you want to pass in the input, and then call the display method. Can.

The following is the sample code:

    /**
     * 内部跳转,用于代替setRedirect. 为什么要这样子做呢?
     * 因为 setRedirect他会发送一个http头到浏览器,让浏览
     * 进行跳转,这样一来就多了一个网络请问, 这是其一。最
     * 为主要的是setRedirect在某些不支持浏览器redirect的情况
     * 下达不到效果,例如:open graphic protocal
     * 
     * @param type $view 要显示的view
     * @param type $layout 要显示的layout, 默认为NULL
     */
    protected function internalRedirect($view, $layout=null){
        $this->input->set("view", $view);
        $this->input->set("layout", $layout);
        return $this->display();
    }
    
    public function checkAvailable(){
        //其他的业务代码
        $this->input->set('tmpl', 'doexam');
        return $this->internalRedirect("doexam", $layout);
    }

The above code is written in your Controller. The function internalRedirect displays the page by setting the view and layout in $input (this input refers to the input parameter of the url), and then directly calling the display method of JControllerLegecy.

In the checkAvailable method, before calling internalRedirect, other parameters required by the view are also set.

A friend of Mengxi said that he encountered such a problem when building one of his websites. We discussed and analyzed the Joomla implementation code and found that the solution is actually quite easy, as long as you are familiar with Joomla component development. If you have any questions, you can talk to me

I hope this article can solve the problems you encounter.

The above introduces the in-depth study of Task and view in Joomla, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn