good day;
I am new in vue.js and I want to build API in my project using vue.js and laravel
I have some question and answer because I got confused
I have services controller that return all service
as below:-
class ServicesController extends Controller
{
public function Services()
{
//get all serveice
$services=Services::where(['deleted'=>1,'status'=>1])->get();
return response()->json($services);
}
}
and API route as below:-
Route::get('/Servicess', 'API\ServicesController#Services');
it is necessary to make a component to send a request to using
Axios request to get data and if yes how to tell the mobile developer about a link to access it.
i want the steps from vue.js side to
prepare data and send it using Axios
You can not use your front-end javascript code inside php controllers, there are two ways to use the data; First: send it via the request. Second: fetch the required data at the back-end side and use it there.
Also there are many alternatives to Axios like the fetch api etc.
Update#1:
example controller
use Illuminate\Http\Request;
class ExampleController extends Controller
{
public function exampleMethod(Request $request){
$name = $request->input('name');
//DO sth
}
}
Route in api.php:
Route::get('/users', 'API\ExampleController#exampleMethod');
Related
I created a component in vue file and I want to fetch data from laravel controller function.
Currently, I have used axios to fetch data. But can I directly call laravel controller function?
Thanks in advance.
No, the only way to communicate with your laravel app is your web service. ( Ex: REST Api )
Because Laravel and Vuejs are completely separated.
Although in using web services you would have different methods.
Expose a route from Laravel/Lumen app
Call the route or url from vue using any http client(axios)
N.B: You cann't call the controller method on Laravel app directly from your Vue CLI
You need to do few steps.
in vue js file script area
const url = `https://www.naveedramzan.com/api/getData/users/4`;
Axios.defaults.headers.common['Authorization'] = `Bearer ${params.token}`; // if you are using some token authentication
Axios.get(url,{params: params})
.then((response) => {
res(response.data);
})
.catch((err) => {
rej(err);
});
In laravel, routes/api.php file
Route::post('getData/users', 'UsersController#getData');
In laravel, the controller file
use Illuminate\Http\Request;
public function getData(Request $request){
....
}
I'm using laravel and integrate the payment gateway.when i send the post request then after success or cancellation ,it redirect the referece id like this
http://localhost/multi_vendor/user/paypal/return?flwref=FLW-MOCK-d4f7572650fbe61ecff7fb17a7129859&txref=rave-bxw7c98dymo8so0kwosco0wwscs8ogc
so how can tackle it in laravel?
I made route for this
Route::get('/paypal/return', 'User\PaypalController#payreturn')->name('user.payment.return');
You can create a route as you write above:-
Route::get('/paypal/return', 'User\PaypalController#payreturn')->name('user.payment.return');
Then in User\PaypalController Controller method payreturn, you can do as following:-
<?php
namespace App\Http\Controllers\User;
use Illuminate\Http\Request;
class PaypalController extends Controller
{
public function payreturn(Request $request)
{
//Here you can get the response request values
$ref = $request->flwref;
$TxRef = $request->txref;
}
}
I also didn't understand well your question, but let me try.
Possible appointments:
if your route is like localhost/multi_vendor/user/paypal/return, you must represent all steps in route file, something like Route::get('/multi_vendor/{user}/paypal/return,[...]). If you are already using somethink like it or a group with prefix, ignore it;
if you wish to redirect, you could use return redirect('yourCompleteUrl') in your controller or in route file;
if you wish to retrieave the parameters Tim Lewis anserwed what you need:
public function payreturn(Request $request)
{
$flref = $request->input('flref');
$txref = $request->input('txref');
}
#darnish manzoor just add your url callback rootverto verifycrsf token
if your redirection url is /ravecallback, just add it and it will stop giving you method not allowed
protected $except = [
'/ravecallback'
];
Is it possible to establish an API endpoint in a laravel voyager hook?
All the documentation I can find shows how to set up a listener for a web request, but these endpoints will not take a post.
Just create the post route in your api routes file.
Example:
// routes/api.php
<?php
Route::post('/webhooks', 'WebhooksController');
// app/Http/Controllers/Api/WebhooksController
public function __invoke(Request $request)
{
// handle webhook
}
Then posting data to yourapp.test/api/webhooks should work.
I am new to CodeIgniter, I want to build restful webservices using CodeIgniter. How can I post data to mysql and fetch from it back using REST services? I have gone through a web site 'tutplus', but it is explained there without mysql database.
Very simply, create some controllers, for example: user.php
At this controller write all necessary methods, exp: getUserInfo(), getUser(), getAge() and call these methods via URL.
To deal with this suggest:
https://ellislab.com/codeigniter/user-guide/general/controllers.html
https://ellislab.com/codeigniter/user-guide/general/routing.html
Actually on the project that i'm working on, we use RESTControllers, there's a project out there on github which extends codeigniter controller with all the REST capabilities:
https://github.com/chriskacerguis/codeigniter-restserver
All you need to do in your code it's include the file on the controller and extend that new controller:
require(APPPATH.'/libraries/REST_Controller.php');
class system extends REST_Controller {
}
You could also include the REST controller on the autoload libraries confinguration.
This library opens the 4 basic REST API as the GET, POST, PUT and DELETE
In your code the controller url should be declarated as this, so you would get a POST method on the index
public function index_post()
{
// ...just some code example
$this->response($book, 201); // Send an HTTP 201 Created
}
If you need a get on the index, you declare it as a GET method:
public function index_get()
{
// ...just some code example
$this->response($book, 201); // Send an HTTP 201 Created
}
I have a standard CI web app, but I've decided to get the chaotic javascript in order using backbone. I had a whole pile of serialized forms/jQuery AJAX requests to various controller methods: authenticate, change_password, register_member, request_new_password, etc.., and don't quite understand how REST works instead. I'm using Phil Sturgeon's REST library for CI https://github.com/philsturgeon/codeigniter-restserver
Should every backbone model have a different api url? And what am I supposed to actually call the controller methods?
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class RestApi extends REST_Controller
{
function get()
{
But it just 404s.
I just don't get how to replace the routing to fifty of my old methods based on a handful of HTTP methods. Does the name of the backbone model need to match something on the server side?
You have to name your functions index_HTTPMETHOD. In your example it would be:
class RestApi extends REST_Controller {
// this will handle GET http://.../RestApi
function index_get() {
}
// additionally this will handle POST http://.../RestApi
function index_post() {
}
// and so forth
// if you want to POST to http://.../RestApi/somefunc
function somefunc_post() {
}
}
the url-attribute of the model should match the server-side 'url' which returns the JSON that will make up the model's attributes. Backbone.js has default functionality to this, which is to match the model's collection url with it's id attribute. The collection url requirement can be foregone by overriding the urlRoot -function, in order to operate model's outside of collections.
If you want to be independent of the id -attribute as well, you sould override the url -attribute/function to provide your own url that matches to the model on the server, like this:
url: 'path/to/my/model'
or
url: function() { // Define the url as a function of some model properties
var path = this.model_root + '/' + 'some_other_url_fragment/' + this.chosen_model_identifier;
return path;
}