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

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

Related

Route my form action into a custom function created inside a resource controller

In my web.php,
Route::get('studentmarksheet/addit','StmarksheetController#addit');
Route::resource('studentmarksheet','StmarksheetController');
I created a custom function addit() inside StmarksheetController. I have a form inside a view where I need to pass values to that addit function. For default functions inside my resoource controller, I used to call by
but, while trying to pass form values in addit function, it says route not defined. What exactly should I write? I have tried
{{route('studentmarksheet/addit')}}
{{route('studentmarksheet#addit')}}
and various other combinations.
I am a total beginner and I don't even know if am questioning this correctly. Please share your answers/suggestions/tips and so on, I would love to read them all.
First you need to name a route
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
Then call it
{{route('stmarksheet.addit')}}
Resource route are named for you by Laravel
You didn't set any name for the addit route. So, you need to write your route as like.
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
And then you can get it in the view.
{{ route('studentmarksheet#addit') }}

What is the method name for restful api in codeigniter?

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

How to use the request route parameter in Laravel 5 form request?

I am new to Laravel 5 and I am trying to use the new Form Request to validate all forms in my application.
Now I am stuck at a point where I need to DELETE a resource and I created a DeleteResourceRequest for just to use the authorize method.
The problem is that I need to find what id is being requested in the route parameter but I cannot see how to get that in to the authorize method.
I can use the id in the controller method like so:
public function destroy($id, DeletePivotRequest $request)
{
Resource::findOrFail($id);
}
But how to get this to work in the authorize method of the Form Request?
That's very simple, just use the route() method. Assuming your route parameter is called id:
public function authorize(){
$id = $this->route('id');
}
You can accessing a Route parameter Value via Illuminate\Http\Request instance
public function destroy($id, DeletePivotRequest $request)
{
if ($request->route('id'))
{
//
}
Resource::findOrFail($id);
}
Depending on how you defined the parameter in your routes.
For my case below, it would be: 'user' not 'id'
$id = $this->route('user');
Laravel 5.2, from within a controller:
use Route;
...
Route::current()->getParameter('id');
I've found this useful if you want to use the same controller method for more than one route with more than one URL parameter, and perhaps all parameters aren't always present or may appear in a different order...
i.e. getParameter('id')will give you the correct answer, regardless of {id}'s position in the URL.
See Laravel Docs: Accessing the Current Route
After testing the other solutions, seems not to work for laravel 8, but this below works
Route::getCurrentRoute()->id
assuming your route is
Route::post('something/{id}', ...)
I came here looking for an answer and kind of found it in the comments, so wanted to clarify for others using a resource route trying to use this in a form request
as mentioned by lukas in his comment:
Given a resource controller Route::resource('post', ...) the parameter you can use will be named post
This was usefull to me but not quite complete. It appears that the parameter will be the singular version of the last part of the resource stub.
In my case, the route was defined as $router->resource('inventory/manufacturers', 'API\Inventory\ManufacturersController');
And the parameter available was manufacturer (the singular version of the last part of the stub inventory/manufacturers)
you will get parameter id if you call
request()->route('id')
OR
$this->route('id')
if you're using resource routing, you need to call with the resource name
// eg: resource
Route::resource('users', App\Http\Controllers\UserController::class);
$this->route('user')
in Terminal write
php artisan route:list
to see what is your param name
Then use
$this->route('sphere') to get param

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!!!

Codeigniter url path

This is the most simple way I can ask this question as I have not fully understood what's going on, or what I am not doing right.
I'm having trouble with the url.
http://localhost/index.php/user is the same as http://localhost/
but
http://localhost/index.php/user/something is not the same as http://localhost/something
How do I make http://localhost/something work?
Does it have to be http://localhost/user/something, how do I make that work?
You need to understand how CodeIgniter's URLs work.
An URL consists of some segments. http://localhost/index.php/user/something/thing In this example user, something and thing are segments of the URL.
Segments of the URL indicate which controller and which method of that controller will run. http://localhost/index.php/user/something/thing In this example the method something from user controller is called and thing is passed to that method as a parameter.
The first segment of the URL indicates the controller.
The second segment of the URL indicates the method of that controller.
The following segments are sent to that method as parameters.
But there are some defaults.
If your URL is http://localhost/index.php/something, you have something specified as the controller, but because you have not specified any method, the default method which is index is called. So the above URL is the same as http://localhost/index.php/something/index
If your URL is http://localhost/index.php/, you don't have any segments specified (no controller and no method). So the default controller which is specified in application\config\routes.php is the loaded controller. Which method of that controller will be called? Of course the index method.
--You can set the default controller by changing $route['default_controller'] = "site"; to what ever fits your application in application\config\routes.php's file.
If you want http://localhost/user/something to be the same as http://localhost/index.php/user/something, you have to create custom routes for your application. More info on that here.
http://localhost/something indicates that you are calling the index method of the Something controller class
http://localhost/user/something indicates that you are calling the something method in the User controller class.
Does that make sense?
In order to make http://localhost/something work, you need a controller called something with an index method. This would be the same as accessing http://localhost/something/index.
Alternatively, http://localhost/user/something implies that you have a user controller with a method called something.
Does that help at all?
To remove index.php from your URL you have to use the mod_rewrite method described here
Then to remove the controller name (user) from the url, you need to use routes
In your case, you would add $route['^(something|something_else|etc)(/:any)?$'] = "user/$0"; to your routes.php file

Categories