doing post call to controller from other php file - laravel

I have a file that i added to my autoload file and i am trying to do a call to a controller function.
The problem is that this function is also called from my .js files and the method i want to call is accepting Input variables which i passed on with tha ajax call. Now my question is how i can call the controller from another php file and pass in post variables.
I can use the Route::post method but how can i pass post variables?
I was told that i can do it through the ioc but i have no idea how this would work.
Thanks

Route::post() is used for registering routes in your app.
Laravel comes with a Client class (from Symfony) that you can use for requests to an url.
But if you want to call a method from another controller, just do $res = FooController::method();

Related

Laravel Route, ->name method?

Laravel 5.5
which is the different doing (no get and post methods) on route defintion web.php file:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController#login');
regarding ->name('') method
Is required define that method? in which cases?
(sample taked from Auth Class definition laravel)
The idea of defining ->name() in routes is for easier code maintenance in the future, it's not mandatory.
Say for example you have few places which uses the route login, one fine day you update the route to user-login. You will have to find and update all the route being used, changing from url('login') to url('user-login').
If you have a route name defined, you will be using route('login'), when you update your route url, there's no need to update all the other files that you're using that route.

codeigniter 404 function is not woking when function calling inside controller

I have set 404_override in route.php so that when there is no correspondent controller it will redirect to my error page
But my question is if there is no function inside the controller it is not redirecting properly
example: http:example.com/search/function_name
here search controller is there but the function_name not exist, how I can redirect to error page in this scenario ?
Your help will be really appreciate!
If you have set up the 404_override properly it will work as intended. You need to pass the controller method in which you are showing your custom 404 page else it would not work.
Create 'index' function in your error controller. Then you do not have to mention the function name in 404_override.

How could I send post parameters with redirect() not using session in CodeIgniter?

How can I send parameters to a post method with redirect() without using a session in CodeIgniter ($this->session->set_flashdata or set_userdata)? In Java I used to use `request.setAttribute('key', 'value');
If I use session it will affect another browser tab... and if I use URL (get) method, it will expose the parameters...
Just use the following pattern:
redirect('controller_name/method_name/' . $parameter_value);

{"status":false,"error":"Unknown method"} in codeigniter

I've managed to setup the REST ful API in my Codeigniter Application.name of controller and name of method are different but {"status":false,"error":"Unknown method"} error was display,how to solve?
Call the domain/index.php/controller/function without the "_get" .For example if your controller is called api and the function is users_get, use the address
localhost/domain/index.php/api/users.
That should work fine.

CodeIgniter: routing & authentication

The idea is to catch any access to controller's functions and, if we are authenticated, rout as normal and, if not, show the login form.
The question is, is _remap function the best place to check for access to controller's functions and how to pass routing back to CI in case we are authenticated?
_remap isn't necessary for this. You could use it, but you don't need to.
Check for access in the __construct() method of the controller. You can get the current method via $this->router->fetch_method() and authenticate against that.
Better yet, have all your controller that need this extend a base controller (aka "MY_Controller"). You can write an Auth_Controller and do the auth check in the __construct() there. You can get the current class via $this->router->fetch_class(), as well as the method, just make sure your controllers that need this extend Auth_Controller instead of the usual CI_Controller.
If they shouldn't have access, just redirect them where they need to go or show an error.
make a library called Authentication and check about your method in this
you can get method and class name by this
$class = $this->CI->router->class;
$method = $this->CI->router->method;
and to check this authentication each time you have enabled the hooks from your config file, attach a post_controller_constructor hook to check authentication each time.

Resources