So, lets say I have a global view and specific view.
In global view, the url may look like this (http://example.com/index.php/controller/method/)
Where when it come to the specific page view, the url will turn like this:
(http://example.com/index.php/controller/method/1989-03-25)
The difference between the global view and the specific page view is, if in the global view it shows the information in general, but in the specific page view it shows based on the detail or the date.
Of course, not only have different view, but also they will have different function of models.
The point is, I just want to make the url keep in order (which it mean there is no change in the name of the controller method).
How to do this. Thanks in advance.
You create just one param into your function. And set the param value is null. like this
class YOUR_CLASS extends CI_controller {
public function method($params=null) //null is Empty value
{
if($params)
{
//load your modal and view with your param
}
else
{
//load your modal and view
}
}
}
This method supports the following type of URL's without any issue.
http://example.com/index.php/YOUR_CLASS/method/
http://example.com/index.php/YOUR_CLASS/method/1989-03-25
Hope this will help you....
This class used to wrap CI_controller, my_base_controller will override CI_controller methods for depends to your project needs
class my_base_controller extends CI_controller {
}
You can load models by known states and define it in my_base_controller class.
class my_controller extends my_base_controller{
public function method($params=null) //null is option value
{
}
}
Good luck!
You can add additional parameter in your method like:
class Your_controller extends CI_controller {
public function method($params = null)
{
// Your Code Here with the condition for processing of the $params variable
}
}
in which that $params can be something in your URL like:
http://example.com/controller/method/your-dynamic-params
So if the $params is null you will call the model the views the general and if the $params has a specific value you can call other model by using if or switch conditional statements. Hope this helps...
Update with Example
you can use the $params variable like this:
if ($params == "1991") {
$this->load->view('general.html', $params);
} elseif ($params == "1992") {
$this->load->view('year_1992.html', $params);
} else {
$this->load->view('other_years.html', $params)
}
in this way you can use the $params as a conditional variable to load different views.
or using switch:
switch($params) {
case '1991':
$this->load->view('general.html', $params);
break;
case '1992':
$this->load->view('year_1992.html', $params);
break;
default:
$this->load->view('other_years.html', $params)
}
Note: Use a helper method so you can avoid fat controllers because it will be hard to test your methods if you have a several lines of codes in a function.
Related
Trying to load different views and methods depending on which view the user is browsing.
Views:
public function edit()
{
if("SOMETHING")return View::make('store_edit');
If("SOMETHING")return View::make('product_edit');
}
Methods:
public function destroy($id)
{
if(SOMETHING){
$store = Store::find($id);
$store->delete();
return redirect('/store');
}
if(SOMETHING){
$product = Product::find($id);
$product->delete();
return redirect('/product');
}
}
What can be used in the if() statements depending on which view is browsed in order to delete the right item and not having to rewrite the functions for each table.
There isn't a simple way to get information about which view was displayed in a previous request, and that's probably not what you want. You should create separate controllers/routes for both "products" and "store". Then you can do away with that view logic altogether.
To somewhat answer your question, you can access information about the current route with the Route facade.
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Read Laravel routing:
https://laravel.com/docs/5.6/routing#route-parameters
Route
Route::get('something/{param}', 'SomeController#edit');
Controller
...
public function edit($param) {
if ($param === $expectedParam) {...} else {...}
}
Also you can make $param optional:
Route
Route::get('something/{param?}', 'SomeController#edit');
Controller (don't forget to give a default value)
...
public function edit($param = null) {
if ($param === $expectedParam) {...} else {...}
}
Please do not use same controller for diferent models.
if has diferent view and diferent functions this HAVE to has diferent controllers
Again, Laravel is a beautiful framework, dont do this!
I'm building an application in Codigniter with HMVC. In it I call a module inside another controller. The problem I've run into is trying to pass/retrieve the data loaded into module. Specifically, I'm loading some javascript files that I would then like to pass to the calling controller.
Here is a simplified code:
public function module()
{
...
$this->data['js'] = $this->js_assets;
...
return $this->load->view('module_view', $this->data, true);
}
public function controller()
{
...
$this->load->module('module/module');
$this->data['module'] = $this->module->module();
...
}
I know that I can retrieve data['js'] in module_view as $js, but I wonder if I can just pass the data directly to the controller.
What I'd like to do is get data from the module to the calling controller
Yes, you can do that with HMVC.
In the calling module you can organise code like this (pseudocode):
class Someclass extends CI_Controller {
...
public function show_page()
{
$this->load->module("module");
$js_files = $this->module->get_js_files();
$this->load->view("header", array("js_files" => $js_files));
}
...
}
In the callee module you would write something along these lines:
class Module extends CI_Controller {
...
public function get_js_files()
{
$scripts = $this->frontend_model->get_scripts();
return $scripts;
}
....
}
(Although in this fantasy case it would be wiser to get data from model in the first place)
Another technique is, like #wolfgang1983 correctly mentioned, to call another module like this:
Modules::run('modulename/controller/function', $data);
and in order to get data from that controller you just have to assign returned data to a variable:
$data_received = Modules::run('modulename/controller/function', $data);
But in this case you can't get variables, only buffered output (like loaded and processed view files or echoed statements).
I want to execute:
$this->view->setVar("menus",$menus);
before the view gets executed.
$menus is an array that can be added by different controllers.
Finally before executing the view i want to put the menus var in the view.
Pick one of the Dispatcher's Events that best suits your needs, then add a method in your controller with same name of the picked event. You can implement this method on your controller base class. For example, adding the $menus in all views for the indexAction:
class MenuControllerBase extends \Phalcon\Mvc\Controller
{
public function beforeExecuteRoute($dispatcher)
{
if($dispatcher->getActionName() == 'index') {
if(isset($this->menus)) {
$this->view->menus = $this->menus;
}
}
}
}
How can i forward to other action inside the same controller avoiding repeat all dispatch proccess ?
Example:
If i point to User Controller the default action is indexAction() inside this funciton i use _forwad('list') ... but all dispatch proccess are repeated.. and i dont that
Whats is the right way ?
Usually, you will install routes to redirect your users to the proper (default) action, instead of the index action (read how to redirect from a given route using Zend_Router). But you can do everything manually if you really want to (however this is called "writing hacker code to achieve something dirty") directly from the controller.
Change your "view script" to be rendered, then call your action method....
// inside your controller...
public function indexAction() {
$this->_helper->viewRenderer('foo'); // the name of the action to render instead
$this->fooAction(); // call foo action now
}
If you tend on using this "trick" often, perhaps you may write a base controller that you extend in your application, which can simply have a method like :
abstract class My_Controller_Action extends Zend_Controller_Action {
protected function _doAction($action) {
$method = $action . 'Action';
$this->_helper->viewRenderer($action);
return $this->$method(); // yes, this is valid PHP
}
}
Then call the method from your action...
class Default_Controller extends My_Controller_Action
public function indexAction() {
if ($someCondition) {
return $this->_doAction('foo');
}
// execute normal code here for index action
}
public function fooAction() {
// foo action goes here (you may even call _doAction() again...)
}
}
NOTE : this is not the official way to do it, but it is a solution.
We Can Also use this Helper To redirect
$this->_helper->redirector->gotoSimple($action, $controller, $module, $params);
$this->_helper->redirector->gotoSimple('edit'); // Example 1
$this->_helper->redirector->gotoSimple('edit', null, null, ['id'=>1]); // Example 2 With Params
If you don't want to re-dispatch there is no reason you can't simply call the action - it's just a function.
class Default_Controller extends My_Controller_Action
{
public function indexAction()
{
return $this->realAction();
}
public function realAction()
{
// ...
}
}
You could also create a route. For example I have in my /application/config/routes.ini a section:
; rss
routes.rss.route = rss
routes.rss.defaults.controller = rss
routes.rss.defaults.action = index
routes.rssfeed.route = rss/feed
routes.rssfeed.defaults.controller = rss
routes.rssfeed.defaults.action = index
Now you only need one action and that is index action but the requess rss/feed also goes there.
public function indexAction()
{
...
}
I am using the following code to initialize a model from within my controller:
$this->load->model('model_name');
Is it possible to modify the above line somehow so that the model constructor recieves a parameter? I want to use the following code in the model constructor:
function __construct($param_var) {
parent::Model();
$this->$param_var = $param_var; //I'm not even sure this works in PHP..but different issue
}
This would be very helpful so that I can reuse my model classes. Thanks.
UPDATE:
(from one of the answers, my original question is solved..thanks!)
Just to explain why I wanted to do this: the idea is to be able to reuse a model class. So basically to give a simple example I would like to be able to pass an "order_by" variable to the model class so that I can reuse the logic in the model class (and dynamically change the order-by value in the sql) without having to create a separate class or a separate function.
Is this poor design? If so could you please explain why you wouldn't do something like this and how you would do it instead?
You can't pass parameters through the load function. You'll have to do something like:
$this->load->model('model_name');
$this->model_name->my_constructor('stuff');
In the model:
function my_constructor($param_var) {
...
}
Response to update:
You could just pass the order_by value when you're calling your model function. I'm assuming in your controller action, you have something like $this->model_name->get($my_id); Just add your order_by parameter to this function. IMO this makes your model logic more flexible/reusable because the way you were doing it, I assume setting order_by in the constructor will set the order_by value for every function.
In model
<?php
/* Load Model core model */
/* BASEPATH = D:\xampp\htdocs\ci_name_project\system\ */
include BASEPATH . 'core\\Model.php';
class User_model extends CI_Model {
/* Properties */
private $name;
/* Constructor parameter overload */
public function __construct($name) {
$this->set_name($name);
}
/* Set */
public function set_name($name) {
$this->name = $name;
}
/* Get */
public function get_name() {
return $this->name;
}
}
in controller
<?php
class User_controller extends CI_Controller {
public function index() {
/* Load User_model model */
/* APPPATH = D:\xampp\htdocs\ci_name_project\application\ */
include APPPATH . 'models\\User_model.php';
$name = 'love';
/* Create $object_user object of User_model class */
$object_user = new User_model($name);
echo $object_user->get_name(); // love
}
}
I see your reasoning for this, but may I suggest looking at Object-Relational Mapping for your database needs. There is a user-made ORM library for CodeIgniter called DataMapper that I've been using lately. You can use tables in your controllers as objects, and it may be a better fit for your problem.
Instead of using DataMapper i suggested to use IgnitedRecord because that the DataMapper is no longer maintained more over it has been replaced into Ruby
I am using CI ver 3.X, so what I am about to say is it will work for Codeigniter 3.X (and I haven't checked ver 4+ yet).
When I went thru the source code of the function model() in file system/libraries/Loader.php, noticed that it does not support loading the model with construct parameters. So if you want to make this happen you have to change the source code (bold, I know, and I just did).
Down below is how I did it.
1. Firstly, replace line 355
$CI->$name = new $model();
with some modifications:
$_args_count = func_num_args();
if(3 < $_args_count){
$refl = new ReflectionClass($model);
$CI->$name = $refl->newInstanceArgs(array_slice($_args_count, 3));
}else{
$CI->$name = new $model(); // origin source code
}
2. Load the model with a bit difference:
$this->load->model("model_name", "model_name", false, $param_var); // where amazing happens
Now you can have $this->model_name as you wished.