I have a file named fn.php in my view, then I create a function to post data, like this
function post($input_name){
return $this->input->post($input_name);
}
In my controller I call it like this,
public function myFunc(){
$this->input->load('fn.php');
// then I use the function that I have created like this
post('myinputname');
}
But... I got error, how I can solve this problem? thank you so much
You should take some time to try and understand the MVC architecture. It is, after all, one of the main points of using a framework.
You can't put functions in a view and expect to load them somehow and access them. You can put functions in a model, controller, library or helper. In you case I would suggest a helper:
application/helpers/some_file_helper.php
function post($input_name){
$CI = &get_instance();
return $CI->input->post($input_name);
}
The get_instance() part is only used when $this (CI context) isn't available. This only happens in helpers and libraries. In views, controllers, and models $this is always available.
Model or controller:
$this->load->helper('some_file');
print_r(post('somevar'));
However if all you want to do is access the post variable use $this->input->post('somevar') directly and don't introduce an extra layer.
Related
I am a new to laravel and trying to understand where the view method comes from and what mechanism allows it to be shown in the web.php folder in laravel.
For example :
Route::get('/', function () { return view('welcome'); })
I guess the view function is defined in some class. Bu which class is it and where is that class made reference to in order to access its method?
Thanks a lot if you can help me understanding this!
In most IDEs you can hold CTRL and left-click the function to view it's definition. view() is not defined in a class. It comes from a file called helpers.php.
This file is included at the beginning, so its functions can be used afterwards.
PHP is not only object oriented. Procedural and object oriented programming can be mixed together.
What I do usually in these cases is to search in the whole project (and remember to include vendor directory in your search) for: "function YOUR_FUNCTION_NAME" because somewhere in PHP there must be that function declared, whether is in a class or in a simple .php file.
view() method is a helper method inside src/Illuminate/Foundation/helpers.php. All the methods that declare here will be available everywhere inside Laravel application. You can check view() method here
In Laravel, are all controllers only supposed to have the basic CRUD methods, as shown in the link below?
https://laravel.com/docs/5.3/controllers#resource-controllers
That is, should the only methods in a controller be:
index()
create()
store()
show()
edit()
update()
destroy()
Thanks.
No.
A controller can have methods named however you want!
If you are creating a RESTful controller, then the names of the methods make sense.
When you create a Resource Controller, then Laravel will save you the pain of writing the routes (you can use Route::resource)
For example: you can do this in YourController.php
function tada() {
return "Tadaaaa";
}
and then in your routes.php, you define a route like
Route::get('tada', 'YourController#tada');
And visiting that route will present you with the string Tadaaaa
Have fun!
No, You can have your own functions too. This is just a boilerplate that Laravel provides you to start with.
In a controller class I have this function:
public function index(){
$this->load->helper('url');
$data['title'] = 'News archive';
$this->load->view('news/index', $data);
}
I load helper url because I'm using anchor() in news/index. So seems like it's enough to load helper in the parent function, and I don't have to load it inside news/index.
So my question is what's going on underneath CI that lets me do this? Is load->view a function, or is it pasting the result of executing news/index on $data? How is load->view aware of helper url having been loaded in index? I'm still trying to make sense of how the CI framework works.
Also what would be the best place to load helper, in the constructor, or in each function as we need it?
Also what would be the best place to load helper, in the constructor, or in each function as we need it?
As a rough rule of thumb;
If you use the helper once in a controller - place it in that specific function
If you use the helper in multiple places in a controller - place it in that controllers constructor
If you use the helper in multiple places in multiple controllers - place it in the 'autoload' section once.
you can get all the answer if u go through the Loader Class in codeigniter...
path >> system/core/loader.php
everything that is done is here....
and for ut last question . according to the user guide
http://ellislab.com/codeigniter/user-guide/libraries/loader.html
loader, as the name suggests, is used to load elements. These elements can be libraries (classes) View files, Helpers, Models, or your own files.
so since it just loads the elements...
i usually(prefer) loading it in each function where needed. (unless i need the same elements in other functions too)
How do I access a controller method from inside a model?
You don't.
Although it is technically possible, if you think that you need to, it suggests a flaw in your application's design.
The Controller layer is the backbone of you application and meant to handle requests from the user, talk to the Model layer, and stitch together the output in the View. Your Model layer should be blind to the Controller and View, but deal with data manipulation only. This is an over-simplified explanation of the MVC pattern (you can find resources for that elsewhere).
Your Codeigniter models should be reusable from any controller, and not dependent on them. There are many solutions to solve whatever problem it is that you have: You can pass data into a model in a number of ways, or you can use the result of a call to a model's method to perform an action in your Controller.
You can use like this:
class some_model extends Model
{
function getController()
{
$controllerInstance = & get_instance();
$controllerData = $controllerInstance->getData();
}
}
My Models in CodeIgniter need to check that a user is authorised to perform the given action. Inside the Models I have been referencing using $this->session->userdata['user_id'].
My question is - should I be setting a variable to $this->session->userdata['user_id'] in the Controller and passing this to the Model, or simply checking it inside the Model ?
Does it even matter ? I suppose passing $user_id into the function would make it (slightly) more readable. What are the arguements and recommendations for / against ?
You can choose between data that is fundamental to your application and data that is incidental to a given model member function. Things that you use everywhere should be guaranteed (base members, globals, etc.), and things used only in the current function should be parameters. You'll find that using implied variables (like $this->session->userdata) in many places in your models and views will become spaghetti quickly, and will be unpredictable if you don't bootstrap them properly.
In my CodeIgniter projects, I add a custom base model and controller that inherit from the CI framework, adding their own member data that is used everywhere in the app. I use these base classes to provide data and functions that all of my models and controllers use (including things like userID). In the constructor of my_base_controller, I call the CI base constructor, and set up data that all of my controllers and views need. This guarantees predictable defaults for class data.
Strictly speaking $this->session->userdata['user_id'] belongs to the controller.
Models deal with data only... controllers, by definition control the flow of the data...
and authentication is a form of data control... (IMHO)
Codewise, I follow this procedure
class MyControllerName extends Controller{
function MyMyControllerName(){
parent::Controller();
$this->_user_id=$this->session->userdata['user_id']; //<-- define userid as a property of class
}
}
And then, say one of my functions foo() requires authentication.. I would do this
function foo(){
$this->_checkAuthentication(); //should short out if not authenticated
//rest of the function logic goes here
}
the _checkAuthentication() can be simplistic like:
function _checkAuthentication(){
if(!isset($this->_user_id) && $this->_user_id<=0){ /or any other checks
header("Location: ".base_url()."location_of/user_not_authorised_page");
exit;
}
}