Create Custom Admin Page in OpenCart
- Article
- Comment (34)
Opencart is one of the popular eCommerce open source. It works under the concept of MVCL . Creating admin page requires a (MVC) control class, model class and view class, additionally one more class for language.
Create files like following structure inside your OpenCart admin folder like the following structure. Here you will see the final result.
admin/controller/kvc/first.php
admin/language/english/kvc/first.php
admin/model/kvc/first.php
admin/view/template/kvc/first.tpl
Here i named custom-folder name as “kvc” and custom-file-name as “kvc.php” . here are the few steps to get your admin page.
1) Create a new file in admin/controller/kvc/first.php
Your file name and controller name should be the same in desc order:
first.php
<?php class ControllerKvcFirst extends Controller{ public function index(){ // VARS $template="kvc/first.tpl"; // .tpl location and file $this->load->model('kvc/first'); // model class file $this->load->language('kvc/first'); //language class file $this->template = ''.$template.''; $this->children = array( 'common/header', 'common/footer' ); $this->response->setOutput($this->render()); } } ?>
2) Create a new file in admin/model/kvc/first.php
<?php class ModelKvcFirst extends Model { public function firstfunction() { //This is for just a demo purpose. $sql = "SELECT x FROM `" . DB_PREFIX . "y`)"; $implode = array(); $query = $this->db->query($sql); return $query->row['total']; } } ?>
3) Create a new file in admin/view/template/kvc/first.tpl
first.tpl
<?php echo $header; ?> <div id="content"> <h1>My First Admin Page</h1> <?php echo 'I can also create a custom admin page.!'; ?> </div> <?php echo $footer; ?>
4) Create a new file in admin/language/english/kvc/first.php
<?php // Heading $_['heading_title'] = 'My First AdminPage'; // Text $_['text_module'] = 'Modules'; $_['text_success'] = 'Success: You have modified module account!'; $_['text_content_top'] = 'Content Top'; $_['text_content_bottom'] = 'Content Bottom'; $_['text_column_left'] = 'Column Left'; $_['text_column_right'] = 'Column Right'; // Entry $_['entry_layout'] = 'Layout:'; $_['entry_position'] = 'Position:'; $_['entry_status'] = 'Status:'; $_['entry_sort_order'] = 'Sort Order:'; // Error $_['error_permission'] = 'Warning: You do not have permission to modify module account!'; ?>
5) You then need to enable the plugin to avoid permission denied errors:
Opencart > Admin > Users > User Groups > Admin > Edit
Select and Enable the Modifiy and Access Permission for kvc/first
Thats it. Now you can access your custom page by the following url
www.yoursite.com/opencart/admin/index.php?route=kvc/first&token= here-your-token-key.
If you want to set the directory permission programmatically Read here.
Dear Varadharajv,
Please, help me, I doit everything like you posted in this link:
https://kvcodes.com/2013/10/create-custom-admin-page-open-cart/#respond
But after when I tried to acces the new page, allways the system is log me out, I got this message “Invalid token session. Please login again.”
I using opencart 1.5.6
Thanks in advance your answer,
Regards,
Ben
hi ben ,
you are missed to do the 5th step. you have to set user access
permission , otherwise it will through a security exception and
ask you to login again.
Thank You for the prompt help, but in time I found what was missed, this I had to add in
admin/controller/common/header.php
$this->data[‘first’] = $this->url->link(‘kvc/first’, ‘token=’ . $this->session->data[‘token’], ‘SSL’);
Link to page admin/view/template/common/header.tpl
<a href="”>First Page
could you please, update this in your tutorial.
Thanks again.
regards,
Ben
error Notice: Error: Could not load language kvc/first! in D:\xampp\htdocs\opencart\system\library\language.php on line 39
You are missed to create kvc/first.php language file. Kindly check ur language directory , if its available there reply me with your code. I will help you.
Notice: Error: Could not load language forms/jumphire! in /home3/leadnex/public_html/ewebsoul.com/projects/php/kangoo/system/library/language.php on line 39
kvc/first.php file is also available
same problem i face plz help.
You missed language file
Check it here
admin/language/english/first/kvc.php
Create Custom Admin Page – OpenCart its for nice too!
Dear Varadharajv, this is a very nice and helpful article.
I have created custom page and it worked in admin
but when i click on it it logs out and asks me to log in
Please update me solution as soon as possbile.
The problem occurs if you have not done the last step. I mean setting user group permissions.
Hi, The function is working nice and fine. I got one more question, thats is how to link the url in the admin page in the menu? Do you have tutorial for this? Please assist. Thanks.
thank you for you feedback. Here is my another article, which will help you to set admin menu link for the custom page. which you create.
https://kvcodes.com/2014/06/how-to-create-admin-menu-link-for-custom-admin-page-opencart/
Hi, The function is working . i just done all the steps including user group permissions also, but still there showing Permission Denied!
try Ben’s code to get access through programmatically.
In the tutorial, you say:
Create a new file in admin/language/kvc/first.tpl
But it’s first.php right? instead of first.tpl
Another point:
The kvc folder must be in the english folder otherwise I have an error.
(admin/language/english/kvc/first.php)
yes. i made a mistake. i will rewrite it.
4) Create a new file in admin/language/english/kvc/first.tpl
should be
4) Create a new file in admin/language/english/kvc/first.php
Sorry, i have updated the corrections. Thank you for mentioning it.
I followed all the above steps but after I completed the last step where I enabled the plugin via Opencart > Admin > Users > User Groups > Admin > Edit, then I tried my custom page and I received “The website cannot display the page HTTP 500” error.
Where could be the problem?
the template
hi
I am using the same code you mention in the page but it shows me Fatal error: Class ‘Controllercustomapp’ not found in C:\wamp\www\opencart\upload\system\engine\action.php on line 62. or blank white page. can you please tell where i am going wrong?
I wrote the article for the opencart 1. 5 versions. But now a days it’s 2 above. So there might be some upgrades and easier functions may came now.
And also the functions points the error in the system files. So there might be alternative way is discovered with core of opencart
How can I link custom inner pages in opencart admin
There after many changes in the Opencart core. i am not sure about the options. but you can do it. It might be reduced complexity.
Hello and thank you for this helpful tutorial,
I’ve one question in addition,
Although new module after setting a permissions works, I’m getting no results with db query from your example and selected table ‘order’
‘class ModelKvcFirst extends Model {
public function firstfunction() {
//This is for just a demo purpose.
$sql = “SELECT x FROM `” . DB_PREFIX . “order`)”;
$implode = array();
$query = $this->db->query($sql);
return $query->row[‘total’];
}
}’
I’ve another example
‘class ModelKvcFirst extends Model {
public function firstfunction() {
$query = “SELECT order_id, customer_id, firstname, lastname, email FROM `order`”;
$value = mysql_query($query) or die(‘Query failed: ‘ . mysql_error());
echo “\n”;
while ($line = mysql_fetch_array($value, MYSQL_ASSOC)) {
echo “\t\n”;
foreach ($line as $col_value) {
echo “\t\t$col_value\n”;
}
echo “\t\n”;
}
echo “\n”;
mysql_free_result($query);
}
}’
But no db results – I’ve also tried echoing values of total, row and query in template but queries are not outputting,
Please reply with your expertise
What is the error you encounter, when you use my code. But i tested it before writing it on my blog.
Hello, same problem here, it does not show anything.
Hello My friend
thank you for this helpful tutorial
I do this example and do not have any error but my last result is a blank page
why ?
you may missed access permisions for the page
No , when I echo every thing in controller file , I see it on output but when I echo every thing in tpl file , I don’t have any result .
Excuse me i am amateur in english .
which means you tpl file has some syntax error or something is missing.