How to get controller method parameters in codeigniter? - php

I have implemented a controller to give access of data of candidates.
class List_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$data[]=array();
/*
Here I want to get value of $area parameter, to save into logs
*/
$userId=$this->session->userdata('userId');
// save logs
$this->UrlAccessHistory->save($this->router->class,$this->router->method,$userId,"Web Application");
}
public function fetchList($area=null){
// fetch list of records from specified area
}
}
$this->router->className gives name of controller Class and $this->router->method gives name of of function called.
Please help to get the list of parameters at location (see comments in code) in the constructor, so that I can store it in the logs.

You could use the URI Class:
$this->uri->segment(n);

I have added a function inside controller to get list of controller parameters
function getControllerParametersFromUri($uri_string){
$list = explode("/",$uri_string);
// skip controller and method names from
// return array list
return array_slice($list,2);
}
// call function by passing uri_string to get parameters list
print_r($this->getControllerParametersFromUri($this->uri->uri_string()));
#Sujit Agarwal $this->uri->segment(3) also works,
but if we need list of all parameters then we may explode uri_string

Related

Saving object in a class variable and using it in another function - php, laravel

So here's the code
use App\Video;
class HomeController extends Controller
{
protected $video;
public function index()
{
// $video_to_watch is fetched from db and I want to save it and use it in
// another function in this controller
$this -> video = $video_to_watch;
return view('home', compact('video_to_watch'));
}
public function feedback(Request $request)
{
dd($this -> video);
}
}
feedback returns null for some reason.
when I put the
dd($this -> video);
in index() it works fine, not null.
I have tried what's suggested here: Laravel doesn't remember class variables
but it didn't help.
I'm sure it's something stupid I'm overlooking. But can't seem to figure out what, any help much appreciated.
You can't keep your $video value between 2 different requests. You have to fetch your video data in each request.
use App\Video;
class HomeController extends Controller
{
public function index() {
$myVideo = $this->getMyVideo();
return view('home', $myVideo);
}
public function feedback(Request $request) {
dd($this->getMyVideo);
}
private function getMyVideo() {
// fetch $video_to_watch from db
return $video_to_watch ;
}
}
First of all don't fetch data inside a Controller. It's only 'a glue' between model and view. Repeat. No fetching inside a controller.
Use domain services and dependency injection to get business data and if you want to share this data create shared service (single instance).
-
Putting a data object into a controller property class makes a temporary dependency between method calls. Avoid it. Use services instead.

Laravel: Strange behaviour of type-hinted class when used twice in controller

I encountered a strange behaviour which I do not understand when using type-hinting in a Laravel Controller. I have a Profile controller, which should call the same function of a service twice based on different parameters. However, when I call the function the second time, the result of the first call is changed.
Here is my code; Two users' profiles should be displayed on one page:
//use statements are omitted for brevity
class ProfileController extends Controller {
protected $compose_profile_service;
public function __construct(ComposeProfileService $compose_profile_service) {
$this->compose_profile_service = $compose_profile_service;
}
public function compare($user1, $user2) {
$user1_profile = $this->compose_profile_service->composeProfile($user1);
$user2_profile = $this->compose_profile_service->composeProfile($user2);
dd($user1_profile); //Problem: This dd() gives me the profile of user2 instead of user1
}
}
class ComposeProfileService {
public function composeProfile($user) {
//Get some stuff from the DB
$profile_data = $user->profile()->get();
return $profile_data;
}
}
Of course, if I dd() $user1 or $user2 above the respective call of the compose function, the right user is provided.

One Method Controller, Multiple View, Codeigniter

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.

pass named arguments from a route to a controller

Given the following Controller:
class Page {
public function about($section){
switch($section){}
}
}
How can I pass the a value to Page->about() directly from $f3->route?
Fat-Free will populate two parameters to each routing handler. So when you got this route:
$f3->route('GET /about/#section','\Page->about'); it will call your function with 1st parameter being the framework instance and 2nd is an array of all routing arguments.
class Page {
public function about($f3, $args){
switch($args['section']){}
}
}
See http://fatfreeframework.com/routing-engine#RoutesandTokens for more details.

Zend passing data to other view

I've tried many solutions that had the same questions like mine. But didn't found a working solution.
I have a controller:
event.php
And two views:
event.phtml
eventList.phtml
I use eventList to get data via ajax call so I want to populate both views with a variable named "eventlist" for example.
Normally I use this code for sending a variable to the view:
$this->view->eventList = $events;
But this variable is only available in event.phtml.
How can I make this available for eventlist.phtml? (without adding a second controller)
Edit:
I get this error now
Call to undefined method Page_Event::render()
Function:
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
$this->eventList = $this->_event;
$this->render();
$this->render('eventlist');
}
If I use $this->view->render('event.phtml') and eventlist.phtml it won't pass the data
I'm using zend version 1
You can pass variables to other views using render()
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
}
Copy and paste this in your controller and rename your controller from event.php to EventController.php
class EventController extends Zend_Controller_Action
{
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
// You're calling the index.phtml here.
$this->eventList = $this->_event;
$this->render('event');
$this->render('eventlist');
}
}
To specify that only written #Daan
In your action:
$this->view->eventList= $events;
$this->render('eventList'); // for call eventList.phtml
In you View use : $this->eventList
You could render it within the view itself (eventList.phtml), rather than within the controller, using the same line of code you used above:
$this->render('event[.phtml]');

Categories