Opencart 4 Extension Route throws 404 - php

I'm trying to develop an Opencart4 Extension and for some reason it's throwing a 404 when I try to access it.
I've the following structure:
/opencart/extensions/myextension/admin/controller/payment/myextension.php
/opencart/extensions/myextension/admin/template/payment/myextension.twig
and here's the controller:
<?PHP
namespace Opencart\Admin\Controller\Extension\MyExtension\Payment;
class MyExtension extends \Opencart\System\Engine\Controller {
public function index() {
$data = array();
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/myextension/payment/myextension', $data));
}
}
Now when I try to access the http://localhost/admin123/index.php?route=extension/myextension/payment/myextension, I get a 404 page!

template must be twig file:
/opencart/extensions/myextension/admin/template/payment/myextension.twig

Related

Codeigniter rest api route issue

I have create a simple rest api but when I hit the path in url it throw an error i.e. {"status":false,"error":"Unknown method"}. I was thinking that there is some problem in my route.php file. How can I solve this problem? Please help me.
controller: User.php
<?php
require APPPATH . '/libraries/REST_Controller.php';
use Restserver\Libraries\REST_Controller;
class User extends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function user_data()
{
$this->db->select('*');
$this->db->from('tbl_books');
$sql = $this->db->get();
$result = $sql->result_array();
$this->response($result, 200);
}
function country()
{
echo "hello";
}
}
route.php
$route['default_controller'] = 'user';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;
$route['user_data'] = "user/user_data";
$route['country'] = "user/country";
you need make 2 changes to your code as below,
change the method signature in your user controller,
from this function country() to this function country_get()
change route for country, you need to add method there.
from this $route['country'] = "user/country"; to this $route['country']['get] = "user/country";
As you said, you are calling
http://localhost/rest/user_data
Try calling
http://localhost/rest/user/user_data
In your case : It is trying to call User_data controller, and since it is not present it is giving you an error.
The first part after your base_path is the controller name followed by method of the controller.
http://localhost/rest/user_data
Eg :
http://localhost/rest/controller/controller_method
Refer :https://www.codeigniter.com/user_guide/general/urls.html

CodeIgniter route is not working, throwing 404 error

I am new to CodeIgniter. I am trying to load a route, but it's not working. Here is my code:
ROUTE:
$route['default_controller'] = 'Welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['register'] = 'Register_controller'; // Not working
$route['register/thankyou'] = 'Register_controller'; // Not working
Register Controller:
class Register_controller extends CI_Controller {
public function index()
{
$this->load->view('register');
}
public function thankyou()
{
$this->load->view('thankyou');
}
}
VIEW :
I HAVE register.php and thankyou.php page on view section
Why is it not working?
It's displaying a CodeIgniter 404 page.
I think you should add method name in controller as following.
$route['register'] = 'Register_controller/index';
$route['register/thankyou'] = 'Register_controller/thankyou';
or
$route['register'] = 'register_controller/index';
$route['register/thankyou'] = 'register_controller/thankyou';

Codeigniter views in main view

I'm loading views in a main view from controller with codeigniter like this
Controller
public function product_modules($domain_id)
{
$this->load->model('/admin/Model_products', '', TRUE);
$data['product_boxes'] = $this->Model_products->getProducBoxes($domain_id);
$this->load->view('admin/dashboard',$data, null, true);
}
Main View
$this->view($_SERVER['REQUEST_URI']);
but if the requested uri is containing query strings, the view is not getting loaded and I get a type Unable to load the requested file: /admin/product_modules/1.php. What would be the best workaround to call views dynamically?
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Example extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
// dynamically assign the method with parameters support
$this->view($method, $params);
}
public function product_modules($domain_id)
{
$this->load->model('/admin/Model_products', '', TRUE);
$data['product_boxes'] = $this->Model_products->getProducBoxes($domain_id);
$this->load->view('admin/dashboard', $data, null, true);
}
public function view($method, $param1 = '', $param2 = '')
{
// params you can sent to models
$data['users'] = $this->model_name->get_user($param1);
// or views
$data['myvar'] = $param2;
// and load the view dynamically
$this->load->view($method, $data);
}
}
So
if the URL is http://example/controller/method then the above will search for method.php view file,
if URL is http://example/controller/product_modules/1 then it search for product_modules.php view file.
Hope this helps. Thanks!!

username as controller codeigniter

I am trying to develop a social network where people can visit mysite.com/username. I used the _remap function and I got it working, however it is not loading any of my other controllers. Can someone be of assistance please?
This is my default controller:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index($username = NULL)
{
$this->load->model('user_model');
if ($this->user_model->is_a_username($username)) {
$data['title'] = $username;
$data['main_content'] = 'users/profile_page';
$this->load->view('shared/template',$data);
} else {
$this->home();
}
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $params);
}
show_404();
}
public function home()
{
if ($this->ion_auth->logged_in()) {
$data['title'] = 'Carnect';
$data['main_content'] = 'users/wall_page';
$this->load->view('shared/template',$data); #if logged in show the user's wall
} else {
$data['title'] = 'Carnect';
$data['main_content'] = 'welcome/index';
$this->load->view('shared/template',$data); #if not logged in show the home page
}
}
}
and this is my routes file:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/create_user";
/*$route['news'] = "news/index";
$route['politics'] = "politics/index";
$route['culture'] = "culture/index";
$route['messages'] = "messages/index";*/
$route['(:any)/(:any)'] = "$1/$2";
$route['(:any)/(:any)/(:any)'] = "$1/$2/$3";
$route['(.*)'] = 'welcome/index/$1';
An example of one of the controllers that will not load..
session_start();
class News extends CI_Controller {
function News()
{
parent::Controller();
}
function index() {
$data['title'] = 'Politics';
$data['main_content'] = 'news/index';
$this->load->view('shared/template',$data);
}
}
I am working on a project that has a similar requirement on those urls.
I did it by adding route like this:
$routes['news'] = 'news/index';
or
$routes['news'] = 'news';
which are exactly lines that you've commented.
Sadly, It isn't possible without those lines (at least I couldn't do it).
If your URL was: example.com/news/index, it would match the rule $routes['(:any)/(:any)'], but if it was example.com/news, it wouldn't match anything and go to your last rule.
CodeIgniter's Routing doesn't take real segments, but the uri segments shown in the url. Therefore, your url example.com/news would be interpreted as a $username = news.
You'd have to do this uri routing for each url which only has 1 uri segment. You'd want to make sure no user has the same username with your controllers or he/she can never visit the user page.
You have to write a rule in application/config/routes.php file just add a following line
$route['(:any)'] = "welcome/index/$1";
You can access by using http://example.com/username if your have added .htaccess rule for removing index.php file from url or you can access like http://example.com/index.php/username

Extending Core Class Codeigniter - 404 Error

I am trying to learn Codeigniter. I am trying to use application/core/MY_Controller in my CI application.
MY_Controller.php as:
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
Now I started to write home controller as :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('pages/'.$page, $data);
}
My views folder as
+pages
+home.php
+templates
+footer.php
+header.php.
Here are my config and route files.
$config['base_url'] = 'http://localhost/~ytsejam/kirmiziblog/';
$config['index_page'] = 'index.php';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
I get 404 page not found error . How can I change my application to work with MY_Controller ?
I also had this problem with extending core classes. My problem was that is was working on my local server, but not on my production server. I was receiving the 404 error in production, and only on my controllers that used the class extensions. After some research I noticed that my local server was running php version 5.3.x, while my production server was running 5.2.x. To fix this I had to upgrade my production server to 5.3.
To find out what version of php your site is using, place this command in your view:
<?php echo phpversion() ?>

Categories