Helper Not Detcteing Subfolder Paths Codeigniter - codeigniter

On my helper function if controller is in parent directory I can load controllers from subfolders
load_controller('subfolder/class');
Problem
If I have a controller that is in a subfolder and want to use this
load_controller('subfolder/class');
I cannot pick it up other controllers if I call them from a controller that is in a sub directory
How can I make it so it will pick up controller which ever controllers and controller subfolder I am in.
Helper
<?php
function load_controller($controller, $method = 'index', $params = '') {
// From Parent Folder "Controllers"
require_once (APPPATH . 'controllers/' . $controller . '.php');
$controllername = basename($controller);
$controllername = new $controllername();
return $controllername->$method($params);
}

Currently your load_controller is as follows:
load_controller($controller, $method , $params){}
It only accepts 3 variables, but you are trying to send location information ('subfolder/class'), which you are not accounting for in your controller.
In order to fix it you need to add another paramater like this:
function load_controller($location, $controller, $method , $params){
require_once (APPPATH .$location. 'controllers/' . $controller . '.php'); //and apply it to the path
}

Related

Laravel Class 'Iyzipay\Options' not found

<?php
namespace App\Http\Controllers\Web;
use Iyzipay\Options;
class IndexController extends Controller
{
.
.
.
public function iyzico(Request $request){
$name = $request->get('name');
$card_no = $request->get('card_no');
$expire_month = $request->get('expire_month');
$expire_year = $request->get('expire_year');
$cvc = $request->get('cvc');
$user = Auth::user();
//options
$options = new Options();
$options->setApiKey("***");
$options->setSecretKey("***");
$options->setBaseUrl("***");
}
.
.
.
}
I'm doing something like this on controller.But I get the error Class 'Iyzipay\Options' not found. I checked the file path and it is correct. No matter what I did I couldn't fix the error
If you want to use iyzipay package, you should add to your composer:
composer require iyzico/iyzipay-php
Edit For Manual Usage:
If you want to use manual you can create Library folder under app directory. And paste iyzipay folder here.
Then create a file such as Iyzipay.php in Library folder.
app
-Library
--iyzipay-php
--Iyzipay.php
And Iyzipay.php (i recommend, don't use transaction process in your controller
<?php
namespace App\Library;
require_once dirname ( __FILE__ ) . '/iyzipay-php/IyzipayBootstrap.php';
class Iyzipay
{
public static function boot ()
{
\IyzipayBootstrap::init ();
}
public static function pay ( $apiInfo, $cartInfo, $price, $shippingTaxPrice = 0 )
{
....
....
}
}
and use in your controller like this:
<?php
...
...
use App\Library\Iyzipay;
...
...
Iyzipay::boot ();
$payment = Iyzipay::pay ( $apiResources, $cartInfo, $price) );

Dynamic Routing (Auto Create Route) on Laravel 5 - View not rendered on App::call()

Basically, I have an admin (CMS) for my app. I have included menu and submenu setup, which are eventually populated on my nav section.
Name: User
URL: setup/user
Icon: fa fa-user
The problem is, if I add new menu (like above), I have to setup another route on web.php, like:
Route::resource('setup/user','UserController');
Well, there will be lots of menu to be created and my web.php is somehow messed up with lots of routes.
I followed this article and it gave me an idea, I tweaked it and created my own.
https://laracasts.com/discuss/channels/laravel/dynamically-calling-a-controller-method-in-laravel-5
Here's what I've done:
function wildcard($route, $controller, $verbs = ['get', 'post']) {
$stack = Route::getGroupStack();
$namespace = end($stack)['namespace'];
$controller = ucfirst(strtolower($route)).'Controller';
Route::match($verbs, rtrim($route, '/') . '/{method}', function($method, $params = null) use ($route, $namespace, $controller)
{
$controller = $namespace . '\\' . $controller;
$method = $controller . '#' . $method;
$params
? App::call($method, explode('/', $params))
: App::call($method);
});
}
wildcard('home',null);
My HomeController:
public function index()
{
return view('pages.common.dashboard');
}
But it doesn't display the view on my index method, just blank page. But it did get the method because I can dump from the method.
Maybe there's a better way on dynamic routing. Thanks.
Solution: You must return your App::call()
Route::match($verbs, rtrim($route, '/') . '/{method}', function($method, $params = null) use ($route, $namespace, $controller)
{
$controller = $namespace . '\\' . $controller;
$method = $controller . '#' . $method;
if($params){
// return the App::call
return App::call($method, explode('/', $params));
} else {
// return the App::call
return App::call($method);
}
});

Trying to check if file_exists in an IF statement

i am trying to check if a file exists, however it is in codeigniter behind /application/views folder.
Is there a way to check for this file OR do I need to move it outside the /application/ folder?
any help would be appreciated?
I am calling the filename from the controller like so:
public function event() {
$data['main_content'] = 'event';
$this->load->view('template', $data);
}
then in the file 'template.php' is where I am making the IF statement for IF file_exists then display that file otherwise do nothing:
<?php
$filename = $main_content;
$exists = file_exists('application/views/scripts' . $main_content);
var_dump($exists); // just for testing
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
if ($exists){
$this->load->view('scripts/'.$main_content.'');
} else{ }
?>
Try the following:
$filename = 'whatever';
$exists = file_exists('application/views/' . $filename);
var_dump($exists); // just for testing
Alternatively, try:
file_exists(realpath(APPPATH . '/views/' . $filename)));
I just tested both ways, both in a controller and a view, and they are working properly.

codeigniter template engine like in Wordpress Or Joomla

Is there a Codeigniter Library or extension which would make possible dynamic templating like in Wordpress or Joomla. What I mean I would like to point my controller to a view which is specified by admin from back.
than I was starting to create by myself but till this point without any success
controller
--main_conroler
here some trying what did not succeed
class MainController extends CI_Controller {
/* Initiate Site
*/
private $method;
private $data;
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('language');
$this->method = $this->router->fetch_method();
if ($this->method == "index") {
$this->data['view'] = 'templates/appStrapp';
} elseif ($this->method != 'site' && method_exists(__CLASS__, $this->method)) {
$this->data['view'] = $this->method;
}
if (empty($this->data['view'])) {
show_404();
}
}
View
view
--templates
---default
----index.php
than here I would like to route my template parts
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
if (file_exists(dirname(__FILE__) . '/tmpl/' . $view . '.php')) {
include ( dirname(__FILE__) . '/tmpl/header.php');
include ( dirname(__FILE__) . '/tmpl/navigation.php');
$this->load->view('site/tmpl/' . $view);
include ( dirname(__FILE__) . '/tmpl/footer.php');
} else {
var_dump('test');
show_404();
}
?>
I've used the following for templates in CI and it's a nice setup.
https://github.com/philsturgeon/codeigniter-template

Codeigniter functions in Smarty

Im try use base_url() and site_url() from a Smarty template.
I read, some articles, about how do this. But, none works.
I create a "plugin" for Smarty called functions.url.php following this tutorial:
https://github.com/EllisLab/CodeIgniter/wiki/Smarty-plugin---URL-Helper
So, i use the {url} Smarty "tag":
<form action={url type='site' url='authentication/login'} method="post" id="login_form">
But, whe i access to the site, smarty shows a large Fatal Error in the below line:
'SmartyCompilerException' with message 'Syntax Error in template
Any ideas ?.
Edit: New steps.
I changed the name of plugin for: plugin.url.php And i try register the plugin in the controller with:
$this->smartyci->registerPlugin("function", "url", "smarty_function_url");
But a new error show:
'SmartyException' with message 'Plugin not callable'
Put Smarty to CI in some folder, for example, third_party/smarty.
Add Smarty to CI - create library application/libraries/Mysmarty.php
define('SMARTY_DIR', APPPATH . 'third_party/smarty/');
require_once(SMARTY_DIR.'Smarty.class.php');
class Mysmarty extends Smarty
{
public function __construct ( )
{
parent::__construct();
$config =& get_config();
$this->template_dir = $config['smarty_template_dir'];
$this->compile_dir = $config['smarty_compile_dir'];
$this->cache_dir = $config['cache_dir'];
$this->caching = $config['caching'];
}
function view($resource_name, $params = array()) {
if (strpos($resource_name, '.') === false) {
$resource_name .= '.tpl';
}
if (is_array($params) && count($params)) {
foreach ($params as $key => $value) {
$this->assign($key, $value);
}
}
if (!is_file($this->template_dir . $resource_name)) {
show_error("template: [$resource_name] cannot be found.");
}
return parent::display($resource_name);
}
}
Add new config variables to application/config/config.php
$config['smarty_template_dir'] = APPPATH . 'views/'; // folder for your smarty templates
$config['smarty_compile_dir'] = APPPATH . 'cache/smarty/compiled/'; // create this folder
$config['cache_dir'] = APPPATH . 'cache/smarty/cached/'; // create this folder
$config['caching'] = 0;
Add new library to autoload in file application/config/autoload.php
$autoload['libraries'] = array('database', 'session', 'mysmarty');
Now in your controller try to add some variable to smarty:
$this->mysmarty->assign('url', $this->config->item('base_url'));
and then show your template:
$this->mysmarty->view('main'); // template path is application/views/main.tpl
And in main.tpl add your form
<form action={$url} method="post" id="login_form">
...
enter code here

Resources