Show all methods name as per controller name selected in codeigniter [closed] - codeigniter

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have 2 dropdown in one of my web page build with CodeIgniter. First dropdown is Controller Name and second one is Method Name. I am able to pull all controllers and showing their names in the dropdown option. Now when user choose a controller name from first dropdown I want to show all methods name declared on that controller file in the second dropdown.
I have tried this below code..
foreach ($controllers as $key => $value) {
foreach(get_class_methods($value) as $methods){
echo $methods;
}
}
here $controllers is an array of all controller name.
above code print only current class methods.i want all the methods of all the controllers.
Any idea how to do that!
so

I've got a link which might give you list of all controllers and methods in those controllers. Hope this might help.
Reference Link

Create a main controller on application/core folder by name My_Controller
And the code for that is,
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function allMethods() {
print_r(get_class_methods($this));
}
}
?>
Which ever class you have to fetch the methods should extend this controller
class A extends Main_Controller { }
class B extends Main_Controller { }
class C extends Main_Controller { }
Now the method allMethods() will be inherited to all controllers
To get all functions from a controller,
Suppose user selects controller A,
Call function like this,,
Suppose user selects controller A,
a/allmethods
Suppose user selects controller B,
b/allmethods
.... so on
So finally,
Get all methods from a controller using,
userselectedcontroller/allMethods
Hope this helps you:)

Related

Modify request data after validation and before sending to controller [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using Laravel's FormRequest for validation to make my code clean in controller and after validation, I want to add some data to request and then save it. So, I was just looking for a solution so that I do not have to add data to request in controller If just after validation in the same file there could be the extra function for modifying data and send to controller. It would have been better.
If you want to do something with request after it validated, you can use After Validation Hook, as apokryfos suggested.
But I think it is a bit more convinient to put that hook inside of your FormRequest descendant class.
Ancestor:
use Illuminate\Foundation\Http\FormRequest;
class AncestorRequest extends FormRequest
{
...
protected function getValidatorInstance()
{
return parent::getValidatorInstance()->after(function ($validator) {
$this->after($validator);
});
}
protected function after($validator)
{
//
}
}
Descendant:
class DescendantRequest extends AncestorRequest
{
...
public function after($validator)
{
// do your things
}
}
P. S. This solution I am using in Laravel 5.2. Here you can find more options.

Include view from another module inside my current view

The structure is as follows:
modules/school/config
modules/school/controllers
modules/school/controllers/form.php
modules/school/models
modules/school/views
modules/school/views/form.php
modules/univ/config
modules/univ/controllers
modules/univ/controllers/form.php
modules/univ/models
modules/univ/views
modules/univ/views/form.php
Now, I don't like the school form anymore, so I want to include university form inside the school view. How do I do that?
$this->load->view('../univ/form'); // does not work
I'm assuming you use HMVC by wiredesignz
in this case you've 2 possibilites
direct call
$this->load->view('univ/form');
or via modules::run
create in your univ/form a function where you load this view - e.g.
class Form extends MX_Controller
{
public funciton view()
{
$this->load->view('form');
}
}
and in your School Class you simply call
class School extends MX_Controller
{
public funciton view()
{
echo modules::run('univ/form/view');
}
}

Laravel - Use Class like Auth

I have a Class Post. Created the Model, Restfull Resource Controller.
Now i implemented the show function in my Resource Controller.
So if someone routes to /post/234234 he will automatically see the post.
But now I want to create a new static function on myself. Let's call it myFunction(). I don't like to hand over the id all the time. Isn't there a possibilty to create a object or something like that, so I can use this like that Post::myFunction(), without hand over the id all the time.
It's similar to the Auth Class. I mean I can check if the user is logged in just like that Auth::check().
Update
Let's describe it more clearly. This is my PostController#show:
public function show($id)
{
//Get board information
$post = Post::find($id);
//Return view
return view('post')->with(array(
'posts' => $post
));
}
It shows just the post. In my view I have a button called Follow up. I only want to show it, if he didn't follow up the post yet. Now i thought about to edit the Model Post.php and add a static function followed() so i can just do this in the view:
#if(Post::followed())
<button>Follow up</button>
#endif
Yes. You can simply create a static function:
public static function myFunction(){
}
Just note that this function will be called in static context so you there's no $this available.
Alternatively you can create a Facade (Auth is a Facade). The process is described pretty well in the documentation about facades
As for your specific case I don't even think a static method is needed. Since you pass $post to the view you can just add a regular public method:
public function isFollowing(){
}
And then do this in your view:
#if(!$post->isFollowing())
<button>Follow up</button>
#endif

Codeigniter organizing controllers structure

Let's take for example a small social network site. One of modules are also Quizzes. Quizzes module have the following sections:
-Create quize
-Edit quize
-Quize view
-Browse quizes
-Send quize to friend
I am wondering what would be the best way in this case. One of option is to create for each module section another controller.
controllers/create_quize.php
controllers/edit_quize.php
controllers/quize_view.php
controllers/browse_quizes.php
controllers/send_quize.php
Another way would be to create single controller for entire module with many functions. None of options are ok. In first way, this could mean to have more than 30 controllers in my controllers folder (having in mind that quizess is just one of many modules). Second option is not ok because a single file will have many functions and won't be easily scanned to developer.
I was also thinking to create many controllers, but organizing them into subfolders. Anyway condeigniter doesn't have this option without modification.
Tnx!
You only need a single controller for your quizzes. Your quiz controller might look like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Quiz extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// Index page. List all quizzes here, perhaps as an replacement to /browse
// /quiz/
}
function create()
{
// Create a new quiz
// /quiz/create
}
function edit($quiz_id)
{
// Edit quiz with $quiz_id
// /quiz/edit/1
}
function browse()
{
// Index page. List all quizzes here, perhaps
// /quiz/browse
}
function send($quiz_id)
{
// Send/share page
// /quiz/send/1
}
}
CodeIgniter is a Model-view-controller framework. You may want to review how it's structured: http://codeigniter.com/user_guide/

Codeigniter : calling a method of one controller from other

I have two controllers a and b.
I would like to call a method of controller a from a method of controller b.
Could anyone help explain how I can achieve this?
This is not supported behavior of the MVC System. If you want to execute an action of another controller you just redirect the user to the page you want (i.e. the controller function that consumes the url).
If you want common functionality, you should build a library to be used in the two different controllers.
I can only assume you want to build up your site a bit modular. (I.e. re-use the output of one controller method in other controller methods.) There's some plugins / extensions for CI that help you build like that. However, the simplest way is to use a library to build up common "controls" (i.e. load the model, render the view into a string). Then you can return that string and pass it along to the other controller's view.
You can load into a string by adding true at the end of the view call:
$string_view = $this->load->view('someview', array('data'=>'stuff'), true);
test.php Controller File :
Class Test {
function demo() {
echo "Hello";
}
}
test1.php Controller File :
Class Test1 {
function demo2() {
require('test.php');
$test = new Test();
$test->demo();
}
}
Very simple way in codeigniter to call a method of one controller to other controller
1. Controller A
class A extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function custom_a()
{
}
}
2. Controller B
class B extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function custom_b()
{
require_once(APPPATH.'controllers/a.php'); //include controller
$aObj = new a(); //create object
$aObj->custom_a(); //call function
}
}
I agree that the way to do is to redirect to the new controller in usual cases.
I came across a use case where I needed to display the same page to 2 different kind of users (backend user previewing the page of a frontend user) so in my opinion what I needed was genuinely to call the frontend controller from the backend controller.
I solved the problem by making the frontend method static and wrapping it in another method.
Hope it helps!
//==========
// Frontend
//==========
function profile()
{
//Access check
//Get profile id
$id = get_user_id();
return self::_profile($id);
}
static function _profile($id)
{
$CI = &get_instance();
//Prepare page
//Load view
}
//==========
// Backend
//==========
function preview_profile($id)
{
$this->load->file('controllers/frontend.php', false);
Frontend::_profile($id);
}
I posted a somewhat similar question a while back, but regarding a model on CI.
Returning two separate query results within a model function
Although your question is not exactly the same, I believe the solution follows the same principle: if you're proposing to do what you mention in your question, there may be something wrong in the way you're coding and some refactoring could be in order.
The take home message is that what you're asking is not the way to go when working with MVC.
The best practice is to either use a Model to place reusable functions and call them in a controller that outputs the data through a view -- or even better use helpers or libraries (for functions that may be needed repeatedly).
You can do like
$result= file_get_contents(site_url('[ADDRESS TO CONTROLLER FUNCTION]'));
Replace [ADDRESS TO CONTROLLER FUNCTION] by the way we use in site_url();
You need to echo output in controller function instead of return.
You can use the redirect() function.
Like this
class ControllerA extends CI_Controller{
public function MethodA(){
redirect("ControllerB/MethodB");
}
}
Controller to be extended
require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');
$report= new onlineAssessmentReport();
echo ($report->detailView());
You can use the redirect URL to controller:
Class Ctrlr1 extends CI_Controller{
public void my_fct1(){
redirect('Ctrlr2 /my_fct2', 'refresh');
}
}
Class Ctrlr2 extends CI_Controller{
public void my_fct2(){
$this->load->view('view1');
}
}
very simple
in first controllr call
$this->load->model('MyController');
$this->MyController->test();
place file MyController.php to /model patch
MyController.php should be contain
class MyController extends CI_Model {
function __construct() {
parent::__construct();
}
function test()
{
echo 'OK';
}
}

Resources