Is it possible in Laravel 4 to define a route that returns either html or json based on if .json is added to the request url?
Eg:
POST example.app/user/new would respond with a Redirect::to another route giving html
POST example.app/user/new.json would respond with a Response::json response giving json
Is it possible to achieve this? And if so how?
You may try:
Route::post('user/new{extension?}', function($extension = null)
{
switch ($extension) {
case '.json':
return Response::json();
break;
default:
return Redirect::to('/');
break;
}
});
Related
Vue component
methods: {
removeCategory(index) {
if (confirm('Are you sure ?')) {
let id = this.categories[index].id;
console.log("ID="+id);
if (id > 0) {
axios.delete('/api/categories/' + id);
}
this.categories.splice(index, 1);
}
},
Routes Api
Route::delete('/categories/{category}', 'CategoryController#destroy');
Controller
public function destroy(Category $category)
{
$this->authorize('delete', $category);
$category->delete();
return $category;
}
Put request is working fine for inserting and updating data, but DELETE request is not able to delete data from the database.
Thanks
Laravel check CSRF token.
You need to add X-CSRF-TOKEN in the request header
You can read this doc : https://laravel.com/docs/7.x/csrf#csrf-x-csrf-token
If you haven't found the answer yet, quick fix as:
Remove the model binding in your Controller.
Grab specific category with an id.
Authorize and delete it.
(no need to modify the Component and the Route)
Is it possible to globally set a listener on API calls made with Axios in Vue? The Laravel back-end has middleware set up on each endpoint that will either give the requested data or return a message saying that they need to check their messages. My goal is to capture that message and redirect the user to the page to view their message. I can't think of a way to do this other than setting something on each function that checks for the message and responds accordingly. There are hundreds of functions and that it wouldn't be a clean solution.
Any and all recommendations are welcome!
Using Axios Interceptors you can do something along these lines:
this.$http.interceptors.response.use(response => () {
// Redirect to a new page when you send
// custom header from the server
if (response.headers.hasOwnProperty('my-custom-header')) {
window.location.href = '/another-page';
}
// Or when you get a specific response status code
if (response.status === 402) {
window.location.href = '/another-page';
}
// Or when the response contains some specific data
if (response.data.someKey === 'redirect') {
window.location.href = '/another-page';
}
// ...or whatever you want
});
i have a controller with 5 methods store / rename / duplicate / move / delete
comming from deferent Forms with POST.
And i want to use Form Request for Validation like that :
any adea on how to validate all my forms without creating a request form file for each Form.
You can use $this->route()->getActionName() to the get the current action. ie. MyController#store, MyController#rename, MyController#delete, ...
Then in your SectionRequest you can do something like this:
public function rules(){
$arr = explode('#', $this->route()->getActionName());
$method = $arr[1]; // The controller method
switch ($method) {
case 'store':
// do something.
break;
case 'rename':
// do something.
break;
case 'delete':
// .... and so
}
}
Is there a way to declare multiple API routes from a single file in Arrow?
Example: Say you want to declare multiple endpoints for a user API:
GET /api/user/:id
DELETE /api/user/:id/delete
POST /api/user
It would make sense to keep these in the same file since they are related and could share code, instead of splitting them into their own files.
I'm referring to these docs.
At this moment the only way to keep them in the same file is to use ALL as the method and then in the action use req.method to delegate to the right logic. E.g.:
..
method: 'ALL',
action: function(req, res, next) {
switch (req.method) {
case 'GET':
..
break;
case 'DELETE':
..
break;
default:
return res.notfound(next);
break;
}
}
..
I have a controller that outputs data from the database in raw JSON format.
I want this to function as an API and allow anyone to make views with any technology that can consume the JSON i.e. Angular, Jquery/Ajax.
However I also want to make a view in Laravel.
So what's the best practice for creating a view from Laravel that uses data from a controller while still allowing the controller to output raw JSON?
The options I'm thinking of are to call the controller from the view(not good?) or to create extra routes.
Route::get('sample', function ()
{
$data = getDataFromSomewhere();
if (Request::ajax())
{
return Response::json($data);
}
return View::make('some.view', compact('data))
});
I would separate API controllers, but you could do something like this if you want one controller to handle all response formats:
URL:
mySite.com/getData?output=json
Controller:
public function index()
{
$data = MyModel::all();
switch(Request::query('output')){
case 'json':
return Response::json($data, $this->responseCode, $this->accessControl);
case 'xml':
return Response::make($data, '200')->header('Content-Type', 'text/xml');
default:
return View::make('data.myData', compact($data));
}
}