I need to create multi domain for my Zend project but I dont know how.
What I want to do is :
The user types "www.mydomain42.tld" and he's on "www.myzendapp.tld/domain/42" without seeing it.
How can I do this ?
Thank you.
You may want to look at adding a method into your Bootstrap.php class to check the domain at initiation and to set these variables in some globally accessible register.
This means you won't need to mess with your routes for every part of the application.
e.g.
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initDomain(){
//Check Url
switch($_SERVER['HTTP_HOST']){
case 'www.example.com':
$id=42;
break;
}
$org = // get org from db?!
Zend_Registry::set('org',$org);
}
}
Related
I have my codeigniter setup with a default controller. It's accessible as follows:
site.com/index.php
But it's actually:
site.com/project/index
Where index is the default function.
I would like to do the following:
site.com/project7362
But it actually wants:
site.com/project/index/project7362
Where project name is a variable that is pass into the index function. But by default it looks for project-name as a controller. Is there a way to avoid this?
Essentially what I'm hoping to accomplish is to pass a variable directly after the domain name. A user may create a project, and I want that project to be accessible at domain.com/project_id
"Essentially what I'm hoping to accomplish is to pass a variable
directly after the domain name. A user may create a project, and I
want that project to be accessible at domain.com/project_id"
so another way to do this would be to have it like.
domain.com/project/id
this will give you much more flexibility later in your routes for adding different features.
in config/routes:
$route['project/(:any)'] = 'project/view/$1';
in your project controller
function view($id) {
// clean it
$id = htmlspecialchars($id) ;
if ( ! $project = $this->members->returnProjectBy($id) {
$this->showNoResultsFor($id) ; }
else { $this->show($project) ; }
}
OR -- another way to do this would be to put your defined routes first, and then have project be last (because it requires searching on whatever is there)
$route['home'] = 'page/home';
$route['contact'] = 'contact';
// etc etc so you first define your hard coded routes, and then if its not any of those
// you do a search on whatever the value is to find a project
$route['(:any)'] = 'project/view/$1';
so then your link could be
domain.com/id
I am interested in having data that can be access through all my views and controllers, but I would like this data to be cleared when the browser is closed or on a logout action.
The reason for this is because I want my views to work only if a variable is set.
eg:
public function adminAction(){
if ($rol_type=='admin'){
$this->renderScript('index/admin.phtml');
}
else{
$this->renderScript('index/adminLogin.phtml');
}
}
I would like also that the admin.phtml view can't be accesed without the variable being set to admin, so that no one can just change the URL and acces admin panel.
I've been reading the zend framework's 2 documentation about session but there is a lot of stuff inside the session module, so I don't know what to use, or where to look for.
I also would be very grateful if you could tell me what is the best way to achive my goal (cause I'm not sure if this is the best way to do what I want to do).
You can use :
use Zend\Session\Container;
In Controller:
$user_session = new Container('mySession');
$user_session->key = "Your Value";
This key can be passed to your view or other models and controllers.
For retrieving we have to do like:
$user_session = new Container('mySession');
$keyValue = $user_session->key; //here you will get the value stored above
Hope that helps
Thanks
I am using codeigniter for a project that is used by a variety of companies.
The default version of our software is up and running and works fine - however some of our customers want slightly different view files for their instance of the system.
Ideally what I would like to do is set a variable (for example VIEW_SUFFIX) and whenever a view file is loaded it would first check if there was a suffix version available if there was use that instead.
For example if the system had a standard view file called 'my_view.php' but one client had a VIEW_SUFFIX of 'client_1' - whenever I called $this->load->view('my_view') if the VIEW_SUFFIX was set it would first check if my_view_client_1 existed (and if it did use that) or if not use the default my_view.php.
I hope that my question is clear enough... If anyone has done this before or can think of a way to do it I would really appreciate it.
EDIT:
Ideally I would like a solution that works without me changing every place that I am calling the view files. Firstly because there are a few files that may want different client versions and also because the view files are called from a lot of controllers
I had a similar requirement for which I created a helper function. Among other things, this function can check for a suffix before loading the specified view file. This function can check for the suffix and check if the file exists before loading it.
Unfortunately, the file checking logic would be a bit brittle. As an alternative, you can implement a MY_Loader class that will override the basic CI_Loader class.
Something like this in your application/core/MY_Loader.php:
class MY_Loader extends CI_Loader {
protected function _ci_load($_ci_data)
{
// Copy paste code from CI with your modifications to check prefix.
}
}
Could you not do this
// some method of creating $client
// probably created at login
$_SESSION['client'] = 'client_1';
$client = (isset($_SESSION['client'])) ? $_SESSION['client'] : '';
$this->load->view("your_view{$client}", $data);
I am currently working on CMS for a client, and I am going to be using Codeigniter to build on top of, it is only a quick project so I am not looking for a robust solution.
To create pages, I am getting to save the page details and the pull the correct page, based on the slug matching the slug in the mysql table.
My question is however, for this to work, I have to pass this slug from the URL the controller then to the model, this means that I also have too have the controller in the URL which I do not want is it possible to remove the controller from the URL with routes?
so
/page/our-story
becomes
/our-story
Is this possible
I would recommend to do it this way.
Let's say that you have : controller "page" / Method "show"
$route['page/show/:any'] = "$1";
or method is index which I don't recommend, and if you have something like news, add the following.
$route['news/show/:any'] = "news/$1";
That's it.
Yes, certainly. I just recently built a Codeigniter driven CMS myself. The whole purpose of routes is to change how your urls look and function. It helps you break away from the controller/function/argument/argument paradigm and lets you choose how you want your url's to look like.
Create a pages controller in your controllers directory
Place a _remap function inside of it to catch all requests to the controller
If you are using the latest version of CI 2.0 from Bitbucket, then in your routes.php file you can put this at the bottom of the file: $routes['404_override'] = "pages"; and then all calls to controllers that don't exist will be sent to your controller and you then can check for the presence of URL chunks. You should also make pages your default controller value as well.
See my answer for a similar question here from a few months back for example code and working code that I use in my Codeigniter CMS.
Here's the code I used in a recent project to achieve this. I borrowed it from somewhere; can't remember where.
function _remap($method)
{
$param_offset = 2;
// Default to index
if ( ! method_exists($this, $method))
{
// We need one more param
$param_offset = 1;
$method = 'index';
}
// Since all we get is $method, load up everything else in the URI
$params = array_slice($this->uri->rsegment_array(), $param_offset);
// Call the determined method with all params
call_user_func_array(array($this, $method), $params);
}
Then, my index function is where you would put your page function.
im developing a web application, using multiple pages, each with their own controller.
The problem now is that there are some variables in a controller, created for one page, that are required for an other page ( with a different controller).
Therefor i need to load one controller into the other one.
I did this by adding
App::import('Controller', 'sections');
$sections= new sectionsController;
$sections->constructClasses();
to the controller, but this doens't seem to work..
Maybe u guys have some ideas?
Thnx in advance!
I think there're some misunderstandings in your mind about MVC architectural pattern.If you need some bullet for your gun,just get the bullet itself,and it's not neccessary to get another gun with it.So I hope you understand loading controller is really a bad idea.
Also if you want some variables accessable by all controllers,as Gaurav Sharma
mentioned ,you can use Configure::write() to store data in the application’s configuration app/config/core.php.e.g
Configure::write('somekey',$someval);
Then you can get $someval by Configure::read('somekey') in any controller.
You can use any one of the methods below to access a variable anywhere in the cakePHP application.
1.) use the configuration class OR
2.) use session for those variables
I've been working on this this morning. I actually get the controller name from a database, but I've changed it to use variables instead.
$controller_name = "Posts"; // the name of the controller.
$action = "index"; // The action we want to call.
App::import('Controller', $controller_name);
// Now we need the actual class name of the controller.
$controller_classname = $controller_name . 'Controller';
$Controller = new $controller_name;
$Controller->variable_name = "something"; // we can set class variables here too.
// Now invoke the dispatcher so that it will load and initialize everything (like components)
$d = new Dispatcher();
$d->_invoke($Controller, array('pass'=> '', 'action' => $action));
// And exit so it doesn't keep going.
exit(0);
I honestly didn't bother figuring out what 'pass' is for (I assume variables), but it throws a warning without it.
You will also need to explicitly call $this->render in your $action.