|
Cms_Controller class
CMS controller class is responsible for pages, partials and templates rendering
It provides methods required for building a website, acting as a glue for website elements.
The instance of the class is created automatically by LemonStand and available in
the code of any page, template and partial as $this variable.
Public properties
Public methods
Events
Property details¶ customer propertypublic Shop_Customer $customer;
A reference to the current customer.
This variable is NULL if there is no customer logged in.
¶ data propertypublic array $data;
A filed for passing variables from actions to pages and partials.
See the Actions page for details.
The contents of this field is automatically converted to PHP variables when
the page is rendered.
See Also¶ page propertypublic Cms_Page $page;
A reference to the current page.
You can access the current object in any CMS template with the following code:
$page = $this->page; ¶ request_params propertypublic array $request_params;
Keeps the page request parameters.
Use request_param() method to access page parameters by index.
Method details¶ action() methodpublic void action()
Executes the page CMS action.
Use this method in custom AJAX handlers to force the controller to execute the default page action.
The following example demonstrates how to execute the page action in an AJAX handler.
function my_handler($controller) { $controller->action(); } See Also¶ create_redirect_url() methodpublic string create_redirect_url()
Creates a redirection URL string suitable for using with redirect_url() method.
A result of this method can be attached to the Login page URL.
Please <a href="<?= root_url('login').'/'.$this->create_redirect_url() ?>">login</a>. See Also¶ css_combine() methodpublic string css_combine(array $files, array $options=array(), boolean $show_tag=true)
Combines and minifies multiple CSS files.
Please read Combining and minifying JavaScript and CSS files
documentation article for details about the method usage.
The $files parameter should contain a list of СЫЫ files to include. The files must be specified
relative to the LemonStand root directory. Remote resources should be prefixed with http:// or https://
protocol specifier. Theme resources should be prefixed with @ symbol. Alongside with the
file paths, the $files array accepts file aliases, which point to LemonStand built-in front-end CSS
libraries:
The following code adds LemonStand front-end CSS file, styles.css file and FancyBox CSS file to the page (the example assumes that FancyBox files have been uploaded to the /resources directory). <head> <?= $this->css_combine(array( 'ls_styles', '/resources/css/styles.css', '/resources/fancybox/jquery.fancybox-1.3.4.css' )) ?> ... ¶ exec_action_handler() methodpublic void exec_action_handler(string $handler)
Executes an AJAX handler directly.
This method is extremely helpful for customizing default server handlers.
It allows to wrap any code around a standard LemonStand handler. The following example
demonstrates a custom AJAX event handler, which can be used for implementing a single-form
checkout process. This code should be entered to the AJAX Handlers field of the AJAX
tab on the Create/Edit Page page.
function my_custom_checkout($controller, $page, $params) { if (!Shop_Cart::list_active_items()) throw new Exception('Your cart is empty.'); $controller->exec_action_handler('shop:on_checkoutSetBillingInfo'); Shop_CheckoutData::copy_billing_to_shipping(); Shop_CheckoutData::set_payment_method( Shop_PaymentMethod::find_by_api_code('no_payment')->id ); Shop_CheckoutData::set_shipping_method( Shop_ShippingOption::find_by_api_code('free_shipping')->id ); Shop_CheckoutData::place_order($controller->customer); Phpr::$response->redirect('/receipt'); } ¶ get_customer() methodpublic static Shop_Customer get_customer()
Returns the current customer object.
If a customer is not logged in, or the script is working out of the context
of a front-end page request, returns NULL.
¶ get_customer_group() methodpublic static Shop_CustomerGroup get_customer_group()
Returns a customer group the current customer belongs to.
If the customer is not logged in, returns the Guest group object.
¶ get_customer_group_id() methodpublic static integer get_customer_group_id()
Returns an identifier of a customer group the current customer belongs to.
If the customer is not logged-in, the method returns an identifier of the Guest group.
The result of this method can be altered by the cms:onGetCustomerGroupId event handler.
¶ get_instance() methodpublic static Cms_Controller get_instance()
Returns the current CMS Controller object.
This method allows to get access to the controller object outside of the
CMS templates. The method returns NULL if there is no CMS Controller
instance available. This means that the script works out of the
front-end page rendering context.
¶ js_combine() methodpublic string js_combine(array $files, array $options=array(), boolean $show_tag=true)
Combines and minifies multiple JavaScript files.
Please read Combining and minifying JavaScript and CSS files
documentation article for details about the method usage.
The $files parameter should contain a list of JavaScript files to include. The files must be specified
relative to the LemonStand root directory. Remote resources should be prefixed with http:// or https://
protocol specifier. Theme resources should be prefixed with @ symbol. Alongside with the
file paths, the $files array accepts file aliases, which point to LemonStand built-in front-end JavaScript
libraries:
The following code adds jQuery, LemonStand jQuery library and FancyBox plugin to the page (the example assumes that FancyBox files have been uploaded to the /resources directory). <head> <?= $this->js_combine(array( 'jquery', 'ls_core_jquery', '/resources/fancybox/jquery.fancybox-1.3.4.pack.js' )) ?> ... ¶ redirect_url() methodpublic string redirect_url(string $default, integer $index=0)
Returns URL of a page which was originally requested before the redirection.
If a not logged on visitor tries to access a page, which can only be accessed by registered customers,
he is redirected to the Login page. This function returns the URL of the original page, which allows
to redirect the customer back to that page after the successful log in.
This method is useful for creating the customer login page.
The following example creates a hidden field with name redirect which is supported
by the shop:login action.
<input type="hidden" value="<?= $this->redirect_url('/') ?>" name="redirect"/> See Also¶ render_block() methodpublic void render_block(string $block_code, string $default=NULL)
Outputs a page block contents.
Use this method for displaying code block defined on the Head & Blocks tab of the Create/Edit Page form.
If the specified block does not exist no error will occur. Please read
this article for details. Example:
<? $this->render_block('sidebar') ?> See Also¶ render_head() methodpublic void render_head()
Outputs the page head declarations block contents.
You can define the header content on the Head & Blocks tab of the Create/Edit Page form.
Please read this article for details.
Example:
<? $this->render_head() ?> ¶ render_page() methodpublic void render_page()
Renders the page.
Call this method inside layouts to output a current page content.
Example:
<? $this->render_page() ?> See Also¶ render_partial() methodpublic mixed render_partial(string $name, array $params=array(), array $options=array( 'return_output'=>false))
Outputs a partial with the specified name.
The following code renders the product_list partial and passes the products variable to it. $this->render_partial('product_list', array('products'=>$category->products));
<? $this->render_partial('my_partial', array(), // No partial-specific parameters (to simplify the example) array( 'cache'=>true,// Enable caching 'cache_vary_by'=>array('url'), // We are going to reuse this partial on different pages, but its content can be different for different pages so we want to create different partial cache versions for different pages 'cache_versions'=>array('catalog'), // The partial should be recached then the catalog updates 'cache_ttl'=>1800 // Recache each 30 minutes ) ) ?> ¶ request_param() methodpublic string request_param(integer $index, mixed $default=NULL)
Extracts a page request parameter from the requested URL by the parameter index.
When LemonStand parses an URL, it considers all URL segments following a page address as parameters.
For example, if there was a page with address /category (the value, specified in the
Page URL field of the Create/Edit Page form), and you opened the page using URL
/category/computers, the word computers would be considered as a parameter with index 0.
The second optional parameter specifies a default value for a parameter. It is used if
a parameter with the specified index is not found.
The $index parameter can take negative values. In this case the method returns index'th parameter from the end of the URL. For example, if the input URL was /category/men/jumpers/2, calling the method with $index=-1 would return 2 (i.e. the first parameter from the end). |