Load images from upload folder into module HMVC codeginiter - codeigniter

I have the next tree in my HMVC application.
-application
-modules
-moduleOne
-assets
-controllers
-models
-views
-uploads
-images
What i need is to load images (From the uploads/images folder) in a view. How can i do that? The assets folder is working fine, but i need to have the uploads folder separated.
I'm using Assets library:
In application/modules/moduleOne/controllers/Assets.php i have:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
// include the global Assets class
include(APPPATH.'libraries/Assets.php');
And in libraries/Assets.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Assets extends MX_Controller {
function __construct() {
parent::__construct();
}
function index(){
//$this->user->authorize();
if(count($this->uri->segments)==2){
show_error("Serving assets for: ".APPPATH. 'modules/' . implode('/', $this->uri->segments));
exit;
}
//---get working directory and map it to your module
$file = APPPATH. 'modules/' . implode('/', $this->uri->segments);
//----get path parts form extension
$path_parts = pathinfo( $file);
//---set the type for the headers
$file_type= strtolower($path_parts['extension']);
if (is_file($file)) {
//----write propper headers
switch ($file_type) {
case 'css':
header('Content-type: text/css');
break;
case 'js':
header('Content-type: text/javascript');
break;
case 'json':
header('Content-type: application/json;charset=UTF-8');
break;
case 'xml':
header('Content-type: text/xml');
break;
case 'pdf':
header('Content-type: application/pdf');
break;
case 'svg':
header('Content-type: image/svg+xml;charset=UTF-8');
break;
case 'jpg' || 'jpeg' || 'png' || 'gif':
header('Content-type: image/'.$file_type);
break;
}
readfile($file);
} else {
show_error("Asset not found: $file");
}
exit;
}
}
It allows me to use assets inside my module. But I need to make the reference in the img src to the uploads/images folder.

Related

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

dynamically page load using codeigniter

helow, I am a newbie in codeigniter. I want to load my pages into my view dynamically. That means, Using a function.
For example i have 4 page home, about, contact, info. In my my view folder I have created a base layout where i include header and footer which is same for all those pages. now i want when i click those page link it just load into content body without loading full page. Using CI normal procedure i can do it. which is
function home(){
$data['main_content'] = 'home';
$this->load->view('template/layout', $data);
}
function about(){
$data['main_content'] = 'about';
$this->load->view('template/layout', $data);
}
but i want to create a function which call this pages into a single function for example,
function pageload($page){
}
this function check that requested file either exits or not exist. if exist it load the page otherwise load "page not found" 404 error page. How can I make it...plz help.
My view file is:
<nav>
<ul class="sf-menu">
<li class="current"><?php echo anchor('home/index', 'Home');?></li>
<li><?php echo anchor('home/amenities', 'Room & Amenitis');?></li>
<li><?php echo anchor('home/reservations', 'Reservations');?></li>
<li><?php echo anchor('home/photos', 'Photographs');?></li>
<li><?php echo anchor('home/pakages', 'Pakages');?></li>
<li><?php echo anchor('home/explore', 'Explore');?></li>
<li><?php echo anchor('home/contact', 'Contact');?></li>
</ul>
</nav>
And Controller:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller{
function __construct(){
parent::__construct();
}
function pageload($page){
$data['main_content'] = $page;
$this->load->view('template/layout', $data);
}
}
?>
You can do something lilke this
function pageload($page){
$data['main_content'] = $page;
$this->load->view('template/layout', $data);
}
if u dont want to show controller_name/pageload in url then in application/config/routes.php
add an array
$route['(home|about_us|other_page|another_page)'] = 'controller_name/pageload/$1';
and to load your 404 page just go to the application/config/routes.php
find this line $route['404_override'] and set its value to snything u want like
$route['404_override'] = 'page_not_found';
then in your controller just add a controller named page_not_found.php and load ur desired 404 custom view from that controller.
Try this function i think this will help you
function build_view($flake) {
$this->data['content'] = $this->load->view($flake, $this->data, TRUE);
$this->load->view('header', $this->data);
}
after you add this you need to add to other function like in index function
function index(){
$this->$data['main_content'] = $page;
$this->build_view('template/layout.php')
}

Anchor Text - Can you hide it? (URL Helper)

Is there anyway to hide the anchor text of the generated by CI? I know I could hide this via CSS (i.e. negative text-indent), but that seems like a lot of unnecessary work. Why wouldn’t I just use a regular HTML coded anchor?
<?php echo anchor(base_url(),''); ?>
Perhaps they thought that people would likely be passing in a blank string more by mistake than by design, I don't know and can't answer that part of your question.
Using the CodeIgniter anchor method in the URL helper has the advantage of automatically adding in your website's base path if necessary.
If you want to keep using the CodeIgniter helper and have anchors with no anchor text, you have several options:
Option 1: Add a space in the second argument:
<?php echo anchor(base_url(),' '); ?>
Option 2: Extend the URL helper and remove the behaviour:
Go into application\helpers and make a new file, called MY_url_helper.php
You can then put code in there to either replace the anchor method or define an entirely new method.
Here are some code examples of what you could put in the file: (I've adapted the code from the url_helper in a CodeIgniter installation I had handy)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('anchor_hide_text'))
{
function anchor_hide_text($uri = '', $title = '', $attributes = '')
{
$title = (string) $title;
if ( ! is_array($uri))
{
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
}
else
{
$site_url = site_url($uri);
}
if ($attributes != '')
{
$attributes = _parse_attributes($attributes);
}
return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
}
}
or
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function anchor($uri = '', $title = '', $attributes = '')
{
$title = (string) $title;
if ( ! is_array($uri))
{
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
}
else
{
$site_url = site_url($uri);
}
if ($attributes != '')
{
$attributes = _parse_attributes($attributes);
}
return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
}

How to use JRouter to rewrite controller and task URLs?

I'm using Joomla 1.5 with router.php file to rewrite URLs of views. Here are contents of router.php:
<?php
function PvcCalcProBuildRoute( &$query )
{
$segments = array();
if(isset($query['view']))
{
$segments[] = $query['view'];
unset( $query['view'] );
}
return $segments;
}
function PvcCalcProParseRoute( $segments )
{
$vars = array();
switch($segments[0])
{
case 'cart':
$vars['view'] = 'cart';
break;
case 'checkout':
$vars['view'] = 'checkout';
break;
case 'login':
$vars['view'] = 'login';
break;
case 'orders':
$vars['view'] = 'orders';
break;
case 'offers':
$vars['view'] = 'offers';
break;
}
return $vars;
}
?>
I need to enable URL rewriting for controller and task. For example this line:
JRoute::_('index.php?option=com_pvccalcpro&task=helpers');
I want to convert to /component/pvccalcpro/helpers.js
And this line:
JRoute::_('index.php?option=com_pvccalcpro&controller=orders&task=js');
I want to convert to /component/pvccalcpro/orders.js
I tried to figure out how to accomplish that with official Joomla Routing Manual, but after spending so many days it still didn't work out for me. Perhaps I'm missing something?
Here are contents of my orders controller for example:
<?php
// no direct access
defined('_JEXEC') or die();
jimport('joomla.application.component.model');
class PvcCalcProControllerOrders extends PvcCalcProController
{
function display()
{
parent::display();
}
function js()
{
$t=time()+31536000;
$expires = gmdate('D, d M Y H:i:s \G\M\T',$t);
header('Content-type: text/javascript; charset=utf-8');
header('Vary: Accept-Encoding');
header('Last-Modified: Tue, 27 Dec 2001 07:05:43 GMT');
header('Expires: '.$expires);
header('Cache-Control: public, max-age=31536000');
require_once(JPATH_COMPONENT.DS.'assets/js/orders.js');
die();
}
}
?>
In your two JRoute examples you're passing a path to your component but expecting it to return a path to your Javascript files.
JRoute is for creating human readable URLs for Joomla! components you are trying to use it for Javascript files by the looks of things.
To quote the Joomla! Doc's page - "Joomla can, initially, only create human readable URLs for built-in components."

How to access Magento from CodeIgniter?

I have a default CodeIgniter 2.1 install with Magento 10.4.4 installed in a subdir called store.
The following code works when run from web root (with .htaccess disabled). It will give the firstname, lastname of the logged in Magento user.
<?php
$site_root = '/var/www/mysite/www/httpdocs';
require_once ($site_root . '/store/app/Mage.php');
umask(0);
// Initialize Magento and hide sensitive config data below site root
$name='frontend';
$options = array('etc_dir' => realpath('../magento-etc'));
Mage::app('default','store', $options);
Mage::getSingleton("core/session", array("name" => $name));
$websiteId = Mage::app()->getWebsite()->getId();
echo "websiteid: $websiteId<br>";
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
echo 'customerwebsiteId: ' . $customer->website_id . '<br>';
$session = Mage::getSingleton('customer/session');
$magento_message = 'Welcome ';
// Generate a personalize greeting
if($session->isLoggedIn()){
$magento_message .= $session->getCustomer()->getData('firstname').' ';
$magento_message .= $session->getCustomer()->getData('lastname').'!';
}else{
$magento_message .= 'Guest!';
}
echo $magento_message;
?>
But, if I run this in a CodeIgniter model, then isLoggedIn returns false.
Here is the CodeIgniter page:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test_mage extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$site_root = '/var/www/mysite/www/httpdocs';
require_once ($site_root . '/store/app/Mage.php');
umask(0);
// Initialize Magento and hide sensitive config data below site root
$name='frontend';
$options = array('etc_dir' => realpath('../magento-etc'));
Mage::app('default','store', $options);
Mage::getSingleton("core/session", array("name" => $name));
$websiteId = Mage::app()->getWebsite()->getId();
echo "websiteid: $websiteId<br>";
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
echo 'customerwebsiteId: ' . $customer->website_id . '<br>';
$session = Mage::getSingleton('customer/session');
$magento_message = 'Welcome ';
// Generate a personalize greeting
if($session->isLoggedIn()){
$magento_message .= $session->getCustomer()->getData('firstname').' ';
$magento_message .= $session->getCustomer()->getData('lastname').'!';
}else{
$magento_message .= 'Guest!';
}
echo $magento_message;
}
}
CodeIgniter is doing something that I have not been able to track yet. The websiteId is returned correctly, but isLoggedIn returns false.
Anyone have any ideas? THANKS!!
I use both but ive never tried to mash them like that. I foresee quite a few problems.
How are you patching into magento?
You might need two db connections running :
$db['magento']
$db['default'] // codeigniter default
Sessions could become a real problem here also aswell as config data.
Consider sticking with magento for now, then maybe patch into your blog/website via a RESTFul service.
Both code examples above work fine. The problem I had was calling session_start() near the top of the CodeIgniter index.php file. Once that was removed, it all started working.
For posterity, here is a Magento 10 Library for CodeIgniter 2.1:
application/libraries/magento.php
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
Class Magento {
function __construct($params)
{
global $site_root;
$name = $params['name'];
// Include Magento application
require_once ($site_root . '/store/app/Mage.php');
umask(0);
// Initialize Magento and hide sensitive config data below site root
// Uncomment next line if you have moved app/etc
// $options = array('etc_dir' => realpath('../magento-etc'));
Mage::app('default','store', $options=null);
return Mage::getSingleton("core/session", array("name" => $name));
}
}
// end of magento.php
Usage example app/model/test_mage.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test_mage extends CI_Controller {
function __construct()
{
parent::__construct();
$params = array('name' => 'frontend'); // frontend or adminhtml
$this->load->library('magento', $params);
}
public function index()
{
$session = Mage::getSingleton('customer/session');
$magento_message = 'Welcome ';
// Generate a personalize greeting
if ($session->isLoggedIn())
{
$magento_message .= $session->getCustomer()->getData('firstname').' ';
$magento_message .= $session->getCustomer()->getData('lastname').'!';
}
else
$magento_message .= 'Guest!';
echo $magento_message . '<br>';
}
}
// end of test_mage.php

Resources