$page in controller calling view file (CodeIgniter) - codeigniter

I have a question,
what does $page purpose in here?
public function user_defined($page = 'view_file.php')
{
#Syntax
}
is $page a user_define too or a fixed// function in CI?
Sorry if this has already an answer, I'm exploring CI

1) if you define parameterized function you have to pass the parameter like below
public function user_defined($page)
{
#Syntax
}
in this case if you call this function without parameter it will respond with fatal error.
2) If you define optional parameterized function you can pass parameter or not
public function user_defined($page = 'view_file.php')
{
#Syntax
}
in this case if you call this function without passing parameter it will respond without any error.

Related

Passing variable to same controller getting error "Too few arguments to function"

In my controller I got error Too few argument to function
public function getpayrollID($id)
{
$fetch2 = DB::table('payroll_details')->where('payroll_daily_id',$id)
->get();
foreach($fetch as $payrollID){
$payroll_id = $payrollID->payroll_daily_id;
}
}
public function payroll($id,$payroll_id)
{
$fetch = DB::table('payroll_attendance')->where('wrk_id',$id)
->where('payroll_daily_id',$payroll_id)
->select(DB::raw('count(wrk_id) as totattn'))
->get();
return $fetch;
}
may route in web.php
Route::get('api/payroll_date/{id}','API\payrollController#payroll');
Route::get('api/payroll_id/{id}','API\payrollController#getpayrollID')
Im using axios get
axios.get('/api/getpayroll_id/'+ this.$route.params.id).then(({data})=>{
(this.payroll_id = data);
axios.get('/api/payroll_date/'+ this.wrk_id).then(({data})=>{
(this.payrolldate = data);
if you want to accrpt ($id,$payroll_id) parameter then u need to accept from route as well
Route::get('api/payroll_id/{id}/{payroll_id}','API\payrollController#getpayrollID')
or if u not sure if second parameter you can mark it optional by ? this symbol like
Route::get('api/payroll_id/{id}/{payroll_id?}','API\payrollController#getpayrollID')
You are geting two variable in your method and you pass one variable in route try this
Route::get('api/payroll_date/{id}/{payroll_id}','API\payrollController#payroll');
I hope this is your answer.
change the routes as :
Route::get('api/payroll_date/{id}/{payroll_id}','API\payrollController#payroll');
Route::get('api/payroll_id/{id}','API\payrollController#getpayrollID')
Besides when you call payroll you should send two argument :
axios.get('/api/payroll_date/'+ this.wrk_id+'/'+payroll_id).then(({data})=>{
(this.payrolldate = data);

Returning same variable to every controller in laravel

I need to send the same result to almost every view page, so I need to bind the variables and return with every controller.
My sample code
public function index()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.index', compact('drcategory','locations'));
}
public function contact()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.contact', compact('drcategory','locations'));
}
But as you see, I need to write same code over and over again. How can I write it once and include it any function whenever I need?
I thought about using a constructor, but I cannot figure out how I can implement this.
You are able to achieve this by using the View::share() function within the AppServicerProvider:
App\Providers\AppServiceProvider.php:
public function __construct()
{
use View::Share('variableName', $variableValue );
}
Then, within your controller, you call your view as normal:
public function myTestAction()
{
return view('view.name.here');
}
Now you can call your variable within the view:
<p>{{ variableName }}</p>
You can read more in the docs.
There are a few ways to implement this.
You can go with a service, a provider or, like you said, within the constructor.
I am guessing you will share this between more parts of your code, not just this controller and for such, I would do a service with static calls if the code is that short and focused.
If you are absolutely sure it is only a special case for this controller then you can do:
class YourController
{
protected $drcategory;
public function __construct()
{
$this->drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
}
// Your other functions here
}
In the end, I would still put your query under a Service or Provider and pass that to the controller instead of having it directly there. Maybe something extra to explore? :)
For this, you can use View Composer Binding feature of laravel
add this is in boot function of AppServiceProvider
View::composer('*', function ($view) {
$view->with('drcategory', DoctorCategory::orderBy('speciality', 'asc')->get());
$view->with('locations', Location::get());
}); //please import class...
when you visit on every page you can access drcategory and location object every time
and no need to send drcategory and location form every controller to view.
Edit your controller method
public function index()
{
return view('visitor.index');
}
#Sunil mentioned way View Composer Binding is the best way to achieve this.

codeigniter child function after submit parent function

In my project I have a controller call load and it has a fuction call search() that has a form, i want to submit this form in a child function of search() like this,,
localhost/ci/index.php/load/search
to
localhost/ci/index.php/load/search/something/
how can i do this after submission a form?
this is my contoller like
class Load extends CI_Controller {
function search() {
$this->load->view('search');
}
function something() {
$this->load->view('something');
}
}
This is not child function something()
You can define routes as per your requirements.
After search function call , you can use redirect() method, that will redirect to your route like 'base_url/controller/search/searchresult/'.
Routes.php
$route['Load/search/searchresult'] = 'Load/something';
Load Controller code :
class Load extends CI_Controller {
function search() {
$this->load->view('search');
// after form submission
redirect('Load/search/searchresult');
}
function something() {
$this->load->view('something');
}
}
This is not a child function of search function so you can call it like
localhost/ci/index.php/load/something/
Add this url to your form action and your form will be submitted to something() function
For clarity: localhost/ci/index.php/load/search/something/ does this...
Enters search() method of Load and provides something as a parameter e.g. search($param = 'something') (this is not correct syntax but just for show).
function search($term = null) {
if (is_null($term)) {
// user hasn't searched
} else {
// user searched for $term e.g. something
}
}
The only way you can achieve what you want is to either use the above somehow to your advantage knowing that /something will have to be the search parameter. Or you can use routing as another answerer suggested.

Codeigniter URI Routing not working till parameter

In my router.php file I added this code.
$route['mission'] = "content/index/mission";
Here as you know content is controller, index is function and mission is parameter to that function.
But when i check it in my browser, it takes me to content/index .
In other words, it is not passing required parameter to index function.
Make sure your recieving the parameters through the function parameters and not using uri segments.
Controller:
// This is incorrect, and will not work
public function index()
{
$param = $this->uri->segment(3); // This wont work
}
// This is correct and will work.
public function index($param = null) // use null to prevent "undefined var error"
{
if($param != null)
{
// The param was passed and do your stuff here
}
}

Codeigniter not taking arguments from URL

I have a function that needs to pull arguments from the URL like CI is supposed to do. But it's not doing it. My URL is domain.com/lasers/en/acme.
My class Lasers is:
class Lasers extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('products_model');
$this->load->model('common_model');
$this->load->model('select_country_model');
$this->load->model('markets_materials_model');
}
function index($lang = NULL, $laser = NULL)
{
$query = $this->products_model->get_product_content($laser, $lang);
}
The model is loaded in the constructor. The $lang I need is "en" and the $laser I need is "acme". So why isn't this working? The arguments in the function are in the correct order, so I can't see what's wrong.
By default you cant pass arguments to the index method of a controller
if you go to domain.com/lasers/en/acme it is looking in the lasers controller for a method called en.. (which doesn't exist) and trying to pass a single parameter of acme to it
Theres a few solutions, probly the easiest is to use a different method (not index) then use routes to make the URLs work.
add something like this to your config/routes.php
$route['^lasers/(:any)/(:any)'] = "lasers/get_products/$1/$2";
Then use a method like this instead of index:
function get_products($lang = NULL, $laser = NULL) {
$query = $this->products_model->get_product_content($laser, $lang);
}
.. OR you could use _remap to override the default behaviour
Does it work if you write "domain.com/lasers/index/en/acme"?
If you write domain.com/lasers/en/acme, it will look for the "En" function, $lang being "acme", and $laser remaining NULL.

Resources