how to include file in zend framework controller? - php

hello I am newbie in zend framework ..
I want to know how to includes files in zend framework Controller
I am using action in zend framework controller like that
public function anyAction()
{
require("../mailchimp/anyfile.php");
}
what I can do to include this files, means where is the right place to kept all this files

This is what the library directory is typically used for. You can put your own functions in there as well as any 3rd party code. Some even put the ZF library in there, but I tend to keep ZF elsewhere on the server and add it to the include_path in php.ini.
With the mailchimp files in your library folder, you can include them like this:
require_once APPLICATION_PATH . '/../library/mailchimp/MCAPI.class.php';
If you add library to your path, then you could shorten it to just:
require_once 'mailchimp/MCAPI.class.php';
You can add library to your path by adding includePaths.library = APPLICATION_PATH "/../library" to your application.ini file. Just be aware of the possible performance penalty you may take by adding additional locations to your include_path.
EDIT:
To display an image that you have stored in public/images, you would use this call from your view/layout:
<img src="<?php echo $this->baseUrl('images/file.jpg') ?>" alt="file.jpg" />
This takes care of figuring out what your base URL is in the event that your Zend Application is not in the root directory of your domain. See the BaseUrl View Helper for more info.

In Zend-Framework you must follow MVC pattern to create any page. when you want to use/call this page you write url using module, controller and action like bellow:
require($this->url(array('module'=>'default','controller'=>'mailchimp','action'=>'mailchimp')));

Related

CodeIgniter Modular Extensions & the i18n library

I have been using the CodeIgniter i18n library by Jérôme Jaglale (http://maestric.com/en/doc/php/codeigniter_i18n), which works great for my project.
But since I need to write separate modules, I recently added CodeIgniter Modular Extensions ( https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc ) and the navigation breaks.
How can I solve this issue please, I would love to use both the i18n library & Modular Extensions.
I think my application navigation fails to work because i18n library introduces adds a language segment in the site url, in my case my url is localhost/index.php/en/home and after adding Modular Extensions, my navigation/links stop working.
Thank you in advance.
Recently, I try to use HMVC with i18n and have similar problem. Below is my solution.
1.first you need to go to here HMVC select "Branches" to download HMVC extension, don't download the one on github it might not work.
2.Unzip HMVC extension inside the folder copy two files "MY_Loader.php" and "MY_Router.php" from core folder to Codeigniter's "application/core" after that copy "MX" folder from "third_party" to Codeigniter's "application/third_party". By this point your HMVC is installed, however it will not work because i18n cause the problem so if you run your website it might not display.
3.You need to get new version of i18n which support both HMVC and none HMVC, the old version of i18n seems not support HMVC. Go to here i18n download it and take time to read the description on github.
4.Before this step I suggest you to backup "application/core/MY_Config.php" and "application/core/MY_Lang.php" in case something goes wrong you can reverst back. Unzip i18n inside folder copy file "language.php" from config folder to Codeigniter's "application/config", copy two files "MY_Config.php" and "MY_Lang.php" from core folder to Codeigniter's "application/core", finally copy "MY_language_helper.php" from helpers folder to Codeigniter's "application/helpers". So far you have new i18n installed, but you need to configure it to make it work, otherwise you might get error message.
5.Open "application/core/MY_Config.php" and replace the line require_once APPPATH . "libraries/MX/Config.php"; to require_once APPPATH . "third_party/MX/Config.php"; then open "application/core/MY_Lang.php" replace the line require APPPATH . "libraries/MX/Lang.php"; to require APPPATH . "third_party/MX/Lang.php";. Why? because it point to the wrong directory, the MX folder is located in "third_party" not "libraries" in case you don't know, if you don't change it you might get error message.
6.To add new language(Not create language file) you need to open "application/config/language.php". You see at top the code block with comment says "Supported Languages" there already have English and Russian language configure for you just need to copy the template and change to the language you want, it's very easy. Be aware folder's name must exactly the same as folder in "application/language".
7.According to i18n github description you need to add these line
$route['^(en|de|fr|nl)/(.+)$'] = "$2";
$route['^(en|de|fr|nl)$'] = $route['default_controller'];
to the "application/config/routes.php". Be aware this line $route['^(en|de|fr|nl)/(.+)$'] = "$2"; in old i18n probably is $route['^(en|de|fr|nl)/(.+)$'] = "$1"; the difference is "$1" have to change to "$2", otherwise you will got problem.
8.To create language file is same as the method you did in old i18n. Now test your website with multi-language to make sure everything work fine.
9.Create your module. How? Create a folder name "modules" inside Codeigniter's application folder, inside modules folder you can start to create your module. That's say you want to create a module call foo, you just need to create a folder name it "foo" and then inside foo folder you can create three folders controllers, models and views. Create a php file with name foo with the code below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Foo extends MX_Controller
{
public function index()
{
echo "<h1>class foo this is module test</h1>";
}
}
Enter url to run your module, if you see "class foo this is module test" then it work.
Remember module class must extends from MX_Controller.
If you still encounter any problem just ask.

ExtJS and autoloading URL

I am using a php framework which requires an index.php file to be used (can be removed with .htaccess, but I would prefer not to do so in my current situation). the problem is that in some cases ExtJS is autoloading classes at URL's like the following
mysite.com/index.php/path/to/file.js
Is there a way to set the URL in which the ExtJS classes will be loaded from?
If you are using app.js and loader then you need to set 2 things:
appFolder property on your Ext.application config
and the path config on the Loader for any other components outside your app structure.
Like this:
Ext.application({
name:'MyApp',
appFolder:'js/MyApp/app'
and
Ext.Loader.setConfig({
enabled:true,
paths: {
'Ext.ux': 'js/extjs/ux'
}

CodeIgniter - extend native library in multiple-site setup

I want to use a central CI setup for multiple sites. The way I handle this is I created a package called MPACK and added it to autoload in the config file of each site.
Folder Structure:
/main
/system (CI 2 System folder)
/MPACK
/site1
/application
site2
/application
Inside this MPACK I have share libraries, models, helpers, etc.
However, I would like to have an extended MY_Form_Validation that would be common to ALL sites. Adding the class file to /MPACK/libraries fails. Adding it to /site1/application works fine, as expected.
Is there any way to do this extending inside MPACK?
Thank you for your time.
Please try this:
// Placed your MY_Form_validation.php under MPACK/libraries
$this->load->add_package_path('/path/to/MPACK');
$this->load->library('form_validation');
You can get more information from CodeIgniter User Guide - Loader Class. :)
You can also autoload your package in /application/config/autoload.php : $autoload['packages'] = array('/path/to/MPACK');
EDIT: turn out that the above solution doesn't work, because Loader always look for APPPATH & BASEPATH first, and I not sure modifying this core class won't break something. Here is another solution in theory:
You should have your MPACK form validation lib, and sites' form validation lib should be symlinks to the MPACK one:
/site1/application/MY_Form_validation.php -> /MPACK/libraries/MY_Form_validation.php
If you just use everything from MPACK, nothing specifically for /site1 or /site2, just make a folder link:
/site1/application/libraries/ -> /MPACK/application/libraries/
Hope this help =)
You can read more here: http://codeigniter.com/wiki/Multiple_Applications_via_Symlinks/

write a file to /public with zend framework

I am trying to create a dynamic sitemap for my site, which runs on Zend.
I want one of my controllers to be able to write to the /public directory, but I can't figure out how to get the file written to the right place.
My relevant directory structure is something like:
.application/
....modules/
.......my_module/
..........controllers/
.............SitemapController.php
.library/
.public/
Is this a simple way in Zend to point at the public/ directory without writing something ugly like "file://../../../public/" every time?
The Zend framework bootstrap defines a constant called APPLICATION_PATH following that guideline you can initialize another constant that points to the public path:
define('PUBLIC_PATH', APPLICATION_PATH . '/../public/');
This will then be available everywhere. Make sure that the webserver has write permissions to the folder you want to store files in.
You can use $_SERVER['DOCUMENT_ROOT'] but you shouldn't -- for security, the public dir shouldn't be writable by the process that runs the web server. There are a number of ways around this. You could write the file to some other stand-alone directory outside the document root and then symlink to it. Or maybe just create a route in your app config so that the file gets generated on the fly every time.

Trouble setting up php Zend include path

I am trying to set up a PHP path to my Zend Framework. I am very confused on how to do this. My Zend Framework is located at the following location on my server:
amazon/ZendFramework-1.10.3-minimal
I am going to be creating a couple of php files in the amazon/ directory that will require the Zend Framework. My include path is:
include("ZendFramework-1.10.3-minimal/library/Zend/Service/Amazon.php");
However inside of Amazon.php is the line
require_once 'Zend/Rest/Client.php';
...and then Client.php has more dependencies set up like that, and so on.
How can I set up my include path so that Amazon.php and Client.php (and so on) can correctly reference the location of the Zend Framework?
Thanks
You will have to set your include path using set_include_path() in your Bootstrap file if you are using any. (need to check the ZF layout for details if you are using the ZF).
The Loading of the classes will be handled by the Zend Loader when you include the library/Zend/Loader.php file and calling the function that will enable the automatic loading of classes that reside in your library/Zend folder.
When you set the include path to your library, include the library/Zend/Loader.php and call Zend_Loader::registerAutoLoad() I believe will be able to work without problems.
Short example in a file called bootstrap.php
set_include_path('ZendFramework-1.10.3-minimal/library/'.get_include_path());
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

Categories