How do you create a custom page in Presta Shop? - php

I have followed many tutorials, but they are either cryptic or they did not work, or they wanted you to use the terrible CMS block module which gives you limited control over the custom elements of a page.
So far:
I created a controller called ProgramsController.php and put it into ps_root/controllers/front
class ProgramsControllerCore extends FrontController
{
public $php_self = 'programs';
public function init()
{
parent::init();
}
public function initContent()
{
parent::initContent();
$this->setTemplate(_PS_THEME_DIR_.'programs.tpl');
}
}
I created a template called programs.tpl and put it into ps_root/themes/mytheme/ folder
I then use: localhost/index.php?controller=programs or I use the SEO and links builder to create a localhost/programs link, and I get an error: Fatal error: Class 'ProgramsController' not found in ...\classes\controller\Controller.php on line 135
But that's not right since the path ought to be a ../controllers/front path, why is it looking in ../classes/controller? I assume from all the tutorials that the dispatcher should know to hook my front controller to the correct template. Why is this not working?
Basic Question:
In PrestaShop 1.6 I just want to know how to create custom pages like: http://myshop.com/mycustompage
But that it also utilizes the existing header and footer.

For creating a new custom page you have to follow some steps:
you have to create a new folder in your modules folder of prestashop like
C:\wamp\www\prestashop\modules\your_module_name
Then you put your controller file in your module folder like:
C:\wamp\www\prestashop\modules\your_module_name\controller\front\your_file.php
And in that you code like:
class ProgramsControllerCore extends FrontController
{
public $php_self = 'programs';
public function init()
{
parent::init();
}
public function initContent()
{
parent::initContent();
$this->setTemplate(_PS_THEME_DIR_.'programs.tpl');
}
}
Then you make a new folder views in your module like:
C:\wamp\www\prestashop\modules\your_module_name\views
Then in that you make another folder named template and in that another a folder named front like:
C:\wamp\www\prestashop\modules\your_module_name\views\templates
C:\wamp\www\prestashop\modules\your_module_name\views\templates\front
Then in that file you have to put your theme file Like
C:\wamp\www\prestashop\modules\your_module_name\views\templates\front\programs.tpl
and now render your file. I hope it would work.

Related

Get name of module in PrestaShop front controller

In PrestaShop (specifically v1.7.5) one can get an instance of the module class by calling
$module = Module::getInstanceByName('theModuleName');
in the controller of a custom module.
Is 'theModuleName' available via some other setting or variable or does it need to be hardcoded?
It should also be used as first parameter to getModuleLink().
You can access the module name (along with the rest from the module class) by:
$theModuleName = $this->module->name;
Using Prestashop core module "Cronjobs" as an example, you can also run module methods inside a front controller like this:
class CronjobsCallbackModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
$this->module->sendCallback();
die;
}
}
If you are working in a child from ProductListingFrontController, this->module is not defined.
If you call the module with getInstanceByName, you get an instance in order to work with it later. The string way doesn´t work in Listing controllers.
class mymoduleMyControllerModuleFrontController extends ProductListingFrontControllerCore
{
public function init()
{
parent::init();
$this -> module = Module::getInstanceByName('mymodule');
}
}

how to retrieve data in view Independent of controller in codeigniter

I have a sidebar in my site that receive some information from db and I can't use controller for retrieve data because I have different controller and same sidebar. How can I print this data in view page.
when I wrote in P.h.P code in the view it shows an error that it cant define variables.
How could I do this?
When you find that you need the same code in many different controllers a "custom library" (class) is the perfect choice. Documentation for creating your own libraries is found HERE.
Controllers should be using models to get data from the database. Custom libraries can also use models just like controllers. Here is a very basic custom library called Sidebar. It depends on a model (sidebar_model) that will not be shown. The purpose of the Sidebar library is to return the variables need by the sidebar_view file.
File: application/libraries/Sidebar.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sidebar
{
protected $CI; // Read the documentation link to see why this is needed.
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->database(); //only needed if not already done
$this->CI->load->model('sidebar_model');
}
public function get_sidebar_data()
{
return $this->CI->sidebar_model->get_sidebar();
}
}
The library method get_sidebar_data() returns the variables for the view.
Here is a controller that uses the custom library. It will use the custom library and a view file (not shown) containing HTML for the sidebar.
File: application/controllers/Main.php
class Main extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('sidebar'); //can also be autoloaded
}
public function index()
{
$data['sidebar'] = $this->sidebar->get_sidebar();
$this->load
->view('banner')
->view('sidebar_view', $data)
->view('main_view')
->view('footer_view');
}
}
Any other controller that needs to show the sidebar would use this same pattern.
This controller loads four different view files and is using "method chaining" which is encouraged. Method chaining executes a tiny bit faster. But the best reason for using it? Less typing.
The method chaning could also be type like this:
$this->load->view('banner')->view('sidebar_view', $data)->view('main_view')->view('footer_view');
But, IMO, putting each ->view() on a separate line makes it easier to read.
You can create a helper for your common tasks. Then create a function for your sidebar and call it where you need it. Check this link for more details about creating helper
You can also create a library for it. Although it will not a very good choice.
create sidebar (view page) and Call model directly inside that sidebar (view page).
secondly call sidebar (view page) directly inside all other view pages.

Embed Yii module page in Facebook app

I've created Yii quiz module which I also want to use in Facebook app (Facebook side).
It can be done trough iframe, but generated page have got also menus and many other unnecessary (in this case) stuff. Is it possible in Yii to show only generated module code without rest of the website?
Try and use a different layout for that module. First you can add a layout folder and layout files to that module, say : /protected/modules/quiz/views/layouts/quizlayout.php. So this new quizlayout.php should be your layout for all the views in this module.
To do that you can set the layout property of the quizmodule in the QuizModule class's init(), like so (in QuizModule.php):
class QuizModule extends CWebModule {
public function init() {
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'quiz.models.*',
'quiz.components.*',
));
$this->layout='quizlayout';
}
//...
}
Now by default, gii generated modules' controllers are subclasses of the Controller class in component/Controller.php file. And that Controller class defines a layout, so if you have that same structure, then the above method will not work, and you'll have to override the layout within your modules' controllers. However instead of going inside each controller and adding a line, you can instead do this in the beforeControllerAction($controller, $action) function in QuizModule.php :
public function beforeControllerAction($controller, $action) {
if(parent::beforeControllerAction($controller, $action)) {
// this method is called before any module controller action is performed
// you may place customized code here
$controller->layout='quizlayout';
return true;
}
else
return false;
}
Edit:
Of course your quizlayout.php should not have code for menus, and any extra stuff, but at the very least the echo $content line should be there, as also mentioned in eskimo's answer.
In you protected/views there is a file called "main.php"
This is your main layout file, that gets rendered around any view called by $this->render
To remove the menu etc.. remove everything within the body except for the line:
<?php echo $content; ?>
Obviously leave in all the stuff in the head (.css files etc...)

CodeIgniter template library problems

I am trying to use a template library with my current codeigniter project. However I am getting the following error message,
Unable to load the requested file: main.php
My file system looks like this,
/application
/themes
/views
/layouts
admin.php
In MY_Controller.php I have the following,
<?php
class Dashboard extends MY_Controller {
public function __construct()
{
parent::__construct();
}
}
?>
In my controller I have the following
<?php
class Dashboard extends MY_Controller {
public function __construct()
{
parent::__construct();
//$this->template->set_layout('admin');
}
public function index()
{
$this->template
->set_layout('admin') // application/views/layouts/two_col.php
->build('welcome_message'); // views/welcome_message
}
}
It depends what template system your using, from the sounds of it you're using Phil Sturgeon's template library.
If so open up config/template.php
and find this line:
$config['layout'] = 'main';
And edit it as applicable.
Which template system are you using? I guess it needs a file named main.php which will load the view. Or the file does not exist and you will have to add it or the path to the file is not correct

How to make a proper index page for subdirectories in CodeIgniter

I'm building an app that has a section for consumers and businesses, and I want to separate the controllers folder appropriately, so it looks like this -
http://domain.com/users/signup/
http://domain.com/business/signup/
I have it working by creating a separate folder for each section in the "controllers" folder, but I want to know how to make an appropriate page when the user visits the http://domain.com/users/. It currently just loads the homepage. How can I fix this?
You don't need to put them in separate folders for this to work.
File system/application/controllers/users.php:
<?php
class Users extends Controller {
function index() {
// this function will be called at http://domain.com/users
}
function signup() {
// this function will be called at http://domain.com/users/signup
}
}
?>
File system/application/controllers/business.php:
<?php
class Business extends Controller {
function index() {
// this function will be called at http://domain.com/business
}
function signup() {
// this function will be called at http://domain.com/business/signup
}
}
?>
If I understand your question correct, you might be looking for URI Routing. I don't see why you would need folders for this. Simply just create the routes you need and make use of the index methods in the controllers for making the "homepage" for both users and business.

Categories