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.
Related
Is there a built in or library based way to implement generic Eloquent/Model based views in Laravel for simple CRUD endpoints?
At the moment I am writing the logic for index, store, destroy, update manually, but all the code is essentially the same.
e.g.
public function destroy($id)
{
$customer= CustomerInfo::find($id);
$customer->delete();
}
I'm more used to Django and the DRF which implements a ModelViewSet class which handles all (most) of the logic for simple CRUD applications.
Implement generic Eloquent/Model based views in Laravel for simple CRUD endpoints
I'm not sure about that but Laravel provides route to model binding, which shortens your code. There's also resource controller which is already pretty much declare all needed methods for you. What's left is quite minimal. So after using model binding and resource controller, your destroy() method may look like:
public function destroy(CustomerInfo $customerInfo)
{
$customerInfo->delete();
}
But the method name and its agrument is already declared like a placeholder, you just write delete() line. More about model binding and resource controller
Use single artisan command to have CRUD endpoints all in one.
php artisan make:controller PhotoController --resource
Because of this common use case, Laravel resource routing assigns the typical create, read, update, and delete ("CRUD") routes to a controller with a single line of code. To get started, we can use the make:controller Artisan command's --resource option to quickly create a controller to handle these actions:
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.
How to load more than one controller in another controller in CodeIgniter. The below code is i'm using. But it doesn't working. Only the controller which specified at first was work the second one is not working.
class A extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->controller('B');
$this->load->controller('C');
}
}
You shouldn't be loading other controllers. Each request should be handled by a single controller. If you require common behaviour you have the following options:
/application/core/MY_Controller.php and extend that class
Move the behaviour to a model
Move the behaviour to a library or helper
If you are unfamiliar with the MVC pattern, this forum post might help you. It's from an old thread, but the principles still apply.
There are various methods to do that.
One of them: you can try this.
//Load the controller you want
$this->load->library('../controllers/controller_name');
//and can call functions of that controller
$this->controller_name->function_name();
I hope this is helpful!
I have UsersController which is RESTful controller, there are some functions within it.
inside UsersController.php:
function postOutletVisit($id){
// some code
}
inside routes.php :
Route::controller('users', 'UsersController');
with this route I can access postOutletVisit action like this:
[POST] mydomain.com/users/outlet-visit/{id}
But I'm wondering if it's possible to convert that link to:
[POST] mydomain.com/users/{id}/outlet-visit
I know that I can do this by defining routes for every action like:
Route::post('users/{id}/outlet-visit', 'UsersController#outletVisit')
But this is not suitable for me because there are plenty of actions inside UsersConroller and I will loose the greate naming convenient for actions (first part of action name determines the method used in it, instead of defining the methods separately in routes.php file)
Outlet Visit is a specific resource, so it doesn't belong in your UserController and should have its own controller.
class OutletController extends BaseController
{
public function postStore($userId) // Controller for storing Outlet Visits
{
...
}
}
Then you define your routes for OutletController, specifying that you need to attach it to a specific user :
Route::controller('users/{id}/outlet', 'OutletController');
If needed you can add more actions for this Controller, such as listing, forms for adding / editing, etc.
First off, I think you mean to use Route::resource('users', 'UsersController').
Secondly, of course you can. Just overload the other route method underneath:
// This defines the predefined Laravel routing
Route::resource('users', 'UsersController')
Route::post('users/{id}/outlet-visit', 'UsersController#outletVisit');
There are no ways to change the urls created by the method Route::controller unless you extend it.
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();
}
}