I'm building a REST API using Laravel 4, and I am having an issue. When I try to send a request to the API in Postman, it will send back errors such as 405, but it simply won't respond to the POST request, which it is designated for. Postman simply says that the server didn't respond. I have tried everything, including changing the code of the controller method to return a plain old 200 code. But, that didn't work either. There is too much code for me to post, but if anyone has any solutions, please let me know. (Btw all other endpoints can be accessed just fine)
My route definition:
Route::group(array(
'name' => 'social-conversation'
), function() {
Route::group(array(
'before' => 'auth'
), function() {
Route::post('social/conversation/create', array(
'as' => 'social-conversation-create',
'uses' => 'Social_ConversationController#createx'
));
Route::put('social/conversation/{id}/update', array(
'as' => 'social-conversation-update',
'uses' => 'Social_ConversationController#update'
));
Route::delete('social/conversation/{id}/delete', array(
'as' => 'social-conversation-delete',
'uses' => 'Social_ConversationController#delete'
));
});
});
My controller definition:
class Social_ConversationController extends BaseController {
public function create() { /* Code goes here */ }
Related
I want to make login with facebook using Socialite in laravel. First I set the route function:
Route::group(['middleware' => ['web']], function(){
Route::get('auth/facebook', [
'as' => 'auth-facebook',
'uses' => 'usersController#redirectToProvider'
]);
Route::get('auth/facebook/callback', [
'as' => 'facebook-callback',
'uses' => 'usersController#handleProviderCallBack'
]);
});
And then I make function in the user controller:
public function redirectToProvider(){
return Socialite::driver('facebook')->redirect();
}
But I'm getting the error The redirect_uri URL must be absolute
Any ideas?
Looks like you are missing the configuration.
You will also need to add credentials for the OAuth services your application utilizes. These credentials should be placed in your config/services.php configuration file, and should use the key facebook, twitter, linkedin, google, github or bitbucket, depending on the providers your application requires. For example:
'facebook' => [
'client_id' => '',
'client_secret' => '',
'redirect' => '',
],
Add callbackURL along with clientID and clientSecret
fb": {
"clientID": "*************************",
"clientSecret": "*******************************",
"callbackURL": "//************/auth/facebook/callback",
"profileFields": ["id", "displayName", "photos"]
},
I have a Laravel 5.2 app that is using Resource routes. I have one as follows:
Route::resource('submissions', 'SubmissionsController');
I want to add a new Post route to it for a sorting form on my index page.
Route::post('submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
I have placed the Post route above my Resource route in my routes.php.
However, a validation Request named SubmissionRequest that is meant for forms within the Submission Resource is being executed on my new Post route. Here is my SubmissionsController Method.
public function index(SortRequest $req)
{
$submission = new Submission;
$submission = $submission->join('mcd_forms', 'mcd_forms.submission_id', '=', 'submissions.id')->where('user_id', Auth::user()->id);
$data['sort_types'] = [
'name' => 'Name',
'form_type' => 'Type'
];
$data['direction'] = ( !empty($req['asc']) ? 'asc' : 'desc' );
$data['dataVal'] = ( !empty($req['sort_type']) ? $req['sort_type'] : 'submissions.id' );
$submission->whereNull('submissions.deleted_at')->orderBy(
$data['dataVal'],
$data['direction']
);
$data['submissions'] = $submission->get();
return view('submissions.index')->with($data);
}
So, when submitting the sorting form from my index page, it is running the SubmissionRequest validation even though I am specifically calling the SortRequest validation. What am I doing wrong?
I resolved it.
Since my Post route was conflicting with my Get route for submissions.index I added below the Resource route the following:
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
This allows the route to accept both Get and Post requests by overriding the automatically generated one.
The documentation is here: https://laravel.com/docs/master/routing#basic-routing
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
in laravel 5 its conflict with #store action
I have a problem when trying to upload an image via bluimp's jQueryFileUpload.
In my routes i have this: Route::post('image/upload/{folder}', 'ImageController#upload');
my file input that is outside the <form> tags because it is independent to the form:
<input id="imageupload" type="file" name="image" multiple="" data-url="{{ url('admin/image/upload/members') }}" >
my jQuery function points to the data-url attribute value.:
$('#imageupload').fileupload({
dataType: 'json',
maxFileSize: 5000000,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
done: function (e, data) {
Members.handle_image(data);
}
});
The weird thing is that when i call this method from example.app/admin/members/create it works, but when i'm trying to access it from example.app/admin/members/1/edit i get a 405, Method not allowed.
In both cases, the Method is POST.
My routes for create and edit URIs:
Route::get('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
I'm sure is something really stupid that i can't see.
PS. I have a Project resource, where i also upload images, using the same route and function. It works on both cases (create and edit).
Anybody had this problem ?
Thank you!
Ok, i managed to solve this, but really i don't understand why it was not working.
In my routes i have this, where the ajax url points as POST:
Route::post('image/upload/{folder}', 'ImageController#upload');
This did not work.
I changed it to:
Route::any('image/upload/{folder}', 'ImageController#upload');
And now it works.
It is strange because on my request headers i have POST method, but with post (in routes) i did not work.
HTTP 405 indicates that the request method is not supported.
Both of your routes listen for get requests
Route::get('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
are you sure that you don't want one, or both to be post?
Route::post('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::post('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
Can we rename routing resource path names in Laravel like in Ruby on Rails?
Current
/users/create -> UsersController#create
/users/3/edit -> UsersController#edit
..
I want like this;
/users/yeni -> UsersController#create
/users/3/duzenle -> UsersController#edit
I want to do this for localization.
Example from Ruby on Rails;
scope(path_names: { new: "ekle" }) do
resources :users
end
I know this is an old question. I'm just posting this answer for historical purposes:
Laravel now has the possibility to localize the resources. https://laravel.com/docs/5.5/controllers#restful-localizing-resource-uris
Localizing Resource URIs By default, Route::resource will create
resource URIs using English verbs. If you need to localize the create
and edit action verbs, you may use the Route::resourceVerbs method.
This may be done in the boot method of your AppServiceProvider:
use Illuminate\Support\Facades\Route;
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot() {
Route::resourceVerbs([
'create' => 'crear',
'edit' => 'editar',
]); }
Once the verbs have been customized, a resource route registration such as Route::resource('fotos', 'PhotoController') will
produce the following URIs:
/fotos/crear
/fotos/{foto}/editar
It ain't pretty, but you could define multiple routes that use the same controller function. For example:
Route::get("user/create", "UsersController#create");
Route::get("user/yeni", "UsersController#create");
The only (glaringly obvious downside) being that you're routes will get quite cluttered quite quickly. There is a setting in app/config/app.php where you can set/change your locale, and it could be possible to use that in conjunction with a filter to use the routes and then group those routes based on the current local/language, but that would require more research.
As far as I know, there isn't a way to rename resource routes on the fly, but if you get creative you can figure something out. Best of luck!
You can't change the resource url's.
For this you will need to define/create each route according your needs
Route::get("user/yeni", "UsersController#create");
and if you need more than one languages you can use the trans helper function, which is an alias for the Lang::get method
Route::get('user/'.trans('routes.create'), 'UsersController#create');
I just had the same issue. And managed to recreate some sort of custom resource route method. It probably could be a lot better, but for now it works like a charm.
namespace App\Helpers;
use Illuminate\Support\Facades\App;
class RouteHelper
{
public static function NamedResourceRoute($route, $controller, $named, $except = array())
{
$routes = RouteHelper::GetDefaultResourceRoutes($route);
foreach($routes as $method => $options) {
RouteHelper::GetRoute($route, $controller, $method, $options['type'], $options['name'], $named);
}
}
public static function GetRoute($route, $controller, $method, $type, $name, $named) {
App::make('router')->$type($named.'/'.$name, ['as' => $route.'.'.$method, 'uses' => $controller.'#'.$method]);
}
public static function GetDefaultResourceRoutes($route) {
return [
'store' => [
'type' => 'post',
'name' => ''
],
'index' => [
'type' => 'get',
'name' => ''
],
'create' => [
'type' => 'get',
'name' => trans('routes.create')
],
'update' => [
'type' => 'put',
'name' => '{'.$route.'}'
],
'show' => [
'type' => 'get',
'name' => '{'.$route.'}'
],
'destroy' => [
'type' => 'delete',
'name' => '{'.$route.'}'
],
'edit' => [
'type' => 'get',
'name' => '{'.$route.'}/'.trans('routes.edit')
]
];
}
}
Use it like this in the routes.php:
\App\Helpers\RouteHelper::NamedResourceRoute('recipes', 'RecipeController', 'recepten');
Where the first parameter is for the named route, second the controller and third the route itself.
And something like this to the view/lang/{language}/route.php file:
'edit' => 'wijzig',
'create' => 'nieuw'
This results in something like this:
This is not possible in Laravel as they use code by convention over configuration. A resources uses the RESTfull implementation
Therefore you have to stick to the convention of
GET /news/create
POST /news
GET /news/1
GET /news/1/edit
...
I'm building an application with AngularJS that talks to a REST API developed in Laravel 4.
I've made a good start so far by having each request send the relevant Access-Control headers that allow Angular to talk to the API, and I've also built a UserController resource:
Route::resource('users', 'UserController');
However now I seem to be facing a wall. For the learning opportunities, I'd like to build this REST API properly using good standards, and I'm trying to work out how to do the responses.
I know I want to return JSON from the API and as far as I know I should wrap every response in a data field, and I also realize I can do that using something like this at the bottom of my controller actions:
return Response::json(array(
'data' => array(
'users' => array(
array(
'username' => 'Bob',
'email' => 'bob#gmail.com'
)
)
),
'status' => 200,
'success' => true
),
200
);
However, I don't want to repeat that kind of logic in every controller action in my application, so I'm wondering how I can make that code more DRY so that at the end of my controller I can just do something like this:
return array(
'users' => array(
array(
'username' => 'Bob',
'email' => 'bob#gmail.com'
)
)
);
and somewhere else in my application I can do the Response::json stuff. Any ideas?
So like you said, you want to return a JSON response, for example:
return Response::json(
array(
'status' => 'success',
'pages' => $modelData->toArray()
),
200
);
So, instead of repeating this everytime, you could just make a createResponse...() function, perhaps in a base controller class, that you can call in all your controllers. A response failed function could look something like:
function createResponseFailed() {
return Response::json([
'error' => [
'message' => 'Some failed message'
]
], 404);
}
That is just a simple example, but illustrates the point. Hope it helps!