What is the method name for restful api in codeigniter? - php

Consider my controller name is Api_example and I have extended REST_Controller.
Now my confusion is
public function user_get(){
//Some code..
}
public function user_post(){
//Some code..
}
Now, I am not able to understand what is the 'user' in that method is. And if I access user_get() method like localhost/api_example/user/get or localhost/api_example/user/post just to display some array data in json format. Its not working.
Please help me.

you only can access to GET method through browser:
localhost/index.php/api_example/user
If you want access to POST method, you must send a post petition, you can read more about POST, GET, PUT and DELETE, here What is difference between HTTP methods GET, POST, PUT and DELETE
the prefix is the name of function, you can name as you want, the important is the sufix GET, POST, PUT or DELETE. index is the name to the default function, the url is [server]/index.php/[controller_name]/[function_name]
For example:
localhost/index.php/api_example/user
localhost is the servername.
index.php is the codeigniter url segment for access to controller folder.
api_example is the name of controller.
user is the name of function (function user_get(){ ... })

You can use following url [for GET request]:
YOUR_BASE_URL/api/example/users

Related

"Undefined type 'App'" in api.php Laravel

I'm using Laravel 8.53 and Vuejs.
I want to set language for specific controller based on api parameter. (to send password reset email in desired language)
I have this route in api.php which works:
Route::post('forgot-password', [NewPasswordController::class, 'forgotPassword'])->name('password.reset');
Now I wanted to create something like this:
Route::post('forgot-password/{locale}', function ($locale) {App::setLocale($locale);}, [NewPasswordController::class, 'forgotPassword'])->name('password.reset');
Now when I send to /api/forgot-password/en I get 200 OK but no response.
My VS Code is showing me this error: Undefined type 'App'.
Do I need to define "App" in api.php? How?
I am not sure if it's correlated but maybe the problem is in different place. You passing three parameters to post method. According to laravel docs you should pass only two. In this case you pass callback, or you pass controller path with method. Try to move App::setLocale() to your controller method and use your first syntax. Remember to import App facade before using it.
// api.php
Route::post('forgot-password', [NewPasswordController::class, 'forgotPassword'])->name('password.reset');
// NewPasswordController.php
use \App;
class NewPasswordController {
public function forgotPassword( $locale ) {
App::setLocale( $locale );
/* rest of code */
}
}

Laravel: Calling to post method function from the same controller in route

I have three functions in my controller.One of them is GET type and other two is POST type. One POST type is working well but how can i call the second POST method from Route?
i am calling my functions from route like this and they are working well
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
there is another function for Deleting from database which is a post method type. Say the Name of that Function is DeletingRecord(). How can i call this function from Route?
Some considerations:
A controller method is not inherently a POST or GET method. It's the router that decides how to handle a POST or GET request.
If you must use a POST request to delete a record then you must assign it to a different route name. Each route will resolve to exactly one method. For example:
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
Route::post('/conference/delete','ViewController#DeletingRecord');
There's no reason why you can't use the DELETE method for this:
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
Route::delete('/conference/home','ViewController#DeletingRecord');
You can use delete HTTP verb.
then your code will look like this:
Route::get('/conference/home', 'ViewController#index');
Route::post('/conference/home','ViewController#showBooking');
Route::delete('/conference/home','ViewController#DeletingRecord');
https://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_Web_services
http://www.restapitutorial.com/lessons/httpmethods.html
why you don't use single routing line instead of more routing line as following:
Route::resource('conference', 'ViewController');
for reference please see following link:
https://laravel.com/docs/5.3/controllers#resource-controllers
i hope its help you

Route::controller with optional parameters in Laravel 4

I'm just new to Laravel but I immediately fell in love with it. As a not so super experienced php developer I do find the official documentation, although very expansive, somewhat complicated to use and find everything I need.
My question is about the Routing component. As the documentation states you can assign a route to a controller with the Route::controller method. So if I want a Blog controller for all /blog/ routes I assign it like this:
Route::controller('blog', 'BlogController');
So then if I'd like to acces all my blog posts I acces the the getIndex method by www.foo.com/blog or www.foo.com/blog/index
But let's say I'd like to be able to display categories via a getCategory method. My url would look like www.foo.com/blog/category and if, for example, I want to get the news category from the DB by slug, I'd like to use: www.foo.com/blog/category/news as the URI.
My question now is, how do I pass the slug to the url and access it in the getCategory method? Do I need specify it via Route::get('blog/category/{slug}', 'BlogController#getCategory') or is there a way to use Route::controller('blog', 'BlogController') and to send and acces parameters from the URL in the getCategory method?
I already tried to find it via google and in the official documentation, but I couldn't find a crystal clear answer to this problem...
You can simply add parameters to your getCategory method:
public function getCategory($category) {
die($category);
}
If you initialize it to null in the parameter list, it becomes optional. Alternatively, you can always pull parameters from the Input object but they would need to be passed in querystring format:
$category = Input::get('category');
With that said, I'd caution against using the Controller route. It's handy and mimics traditional MVC frameworks, but I believe it's planned to be deprecated -- and honestly, you miss out on some pretty flexible features.
using Route::controller('blog', 'BlogController'); allows you to define a single route to handle every action in a controller using REST naming conventions.then you have to add methods to your controller, prefixed with the HTTP verb they respond to. That means if you have a method called getIndex() it will be executed when there is a GET request to the url "yoursite.com/blog".
To handle POST requests to the same url add a method prefixed with post(ex: postComment()) and so on for other http verbs PUT, PATCH and DELETE.
I think you want something more customized, so you can use a resource controller:
Route::resource('blog', 'BlogController');
This will generate some RESTful routes around the blog resource, run php artisan routes in your project folder to see the generated routes, it should be something like this:
Verb Path Action Route Name
GET /blog index blog.index
GET /blog/create create blog.create
POST /blog store blog.store
GET /blog/{blog} show blog.show
GET /blog/{blog}/edit edit blog.edit
PUT/PATCH /blog/{blog} update blog.update
DELETE /blog/{blog} destroy blog.destroy
in the action column are the functions that you should have in the controller.
If you want to define more routes you can simply do it with Route::get or Route::post in the routes.php file
I hope this will make it more clear for you, enjoy routing with Laravel!!!

What is required to call a controller in Codeigniter?

I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.

CodeIgniter: Page not found when passing parameters to a controller?

I'm trying to pass parameters to a control in codeigniter, but I'm getting 404 page not found error, I don't get it, I did what the guide says: http://codeigniter.com/user_guide/general/controllers.html#passinguri
When I remove the params in the index function and just access the controller everything works fine, but I can't pass a value to it...
Here is the code the way I'm trying to send a param:
http://mysite/123
<?php
class Main extends Controller {
function index($username) {
echo $username;
}
}
?>
How can I get more info regarding this error from codeigniter?
Thank you.
With that URL, CodeIgniter can't understand if you want to pass 123 to the index method or if you're requesting the 123 method with no parameters. You have to explicitly name the default method if you need to pass it some parameters.
http://mysite/index/123
Option 1 - Rempap the function call in your controller
If your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.
http://codeigniter.com/user_guide/general/controllers.html#remapping
Option 2 - Use a custom route.
http://codeigniter.com/user_guide/general/routing.html

Categories