I'm trying to add a new controller to an existing laravel project. The application already has some pages at /users and I am trying to add a RESTful API which works separately to this. I would like the API to be available at api/users.
I have created the controller using PHP artisan:
php artisan controller:make ApiUsersController
I have added the following to my routes:
Route::controller('api/users', 'ApiUsersController');
However when I hit the URL I just receive the site's 'Page could not be found' message.
Is there something I have missed?
It looks like the issue you're having is that you've used Route::controller rather than Route::resource.
Route::resource maps routes to the seven RESTful methods that the controller generator creates by default. Route::controller maps them to methods that you add yourself that have the HTTP method as part of their name, in your case if you had a method called getIndex it would be called on a GET request to /api/users/index or if you had one called postStore it would be called on a POST request to /api/users/store.
In order to add the API prefix to the route you could use the following:
Route::group(['prefix' => 'api'], function() {
Route::resource('users', 'ControllerName');
});
You could also add any other controllers in the API within the same callback.
Related
I want to create a custom public API; and answer all api requests under ApiController with a routing like this:
Route::resource("/api","ApiController");
I tried to add this under routes/web or routes/api; but no chance. I get "Sorry..page not found".
Other routing options under routes/web work fine; I only have a problem when it comes to /api.
Should I proceed with a route like /custom_public_api or is there anything I can do about this?
If you put this in your api.php
Route::resource("/users","UserController");
Than the routes will be automatically prefixed with /api.
So the routes will look like this:
/api/users
/api/users/{user}
...
So in your case it is not working because you have this type of routes:
/api/api
/api/api/{api}
/api/api/{api}/edit
...
So you're having api twice. So you just have to suppose that the api prefix, is automatically added from routes/api.php.
For more information about your routes, you can run php artisan route:list and you can check how do your routes look like.
For api resources you have to use
Route::apiResource('photos', 'PhotoController');
as says in the documantation here (just scroll a little until you get to API Resource Routes)
I'm building enterprise modular Laravel web application but I'm having a small problem.
I would like to have it so that if someone goes to the /api/*/ route (/api/ is a route group) that it will go to an InputController. the first variable next to /api/ will be the module name that the api is requesting info from. So lets say for example: /api/phonefinder/find
In this case, when someone hit's this route, it will go to InputController, verifiy if the module 'phonefinder' exists, then sends anything after the /api/phonefinder to the correct routes file in that module's folder (In this case the '/find' Route..)
So:
/api/phonefinder/find - Go to input controller and verify if phonefinder module exists (Always go to InputController even if its another module instead of phonefinder)
/find - Then call the /find route inside folder Modules/phonefinder/routes.php
Any idea's on how to achieve this?
Middlewares are designed for this purpose. You can create a middleware by typing
php artisan make:middleware MiddlewareName
It will create a middleware named 'MiddlewareName' under namespace App\Http\Middleware; path.
In this middleware, write your controls in the handle function. It should return $next($request); Dont change this part.
In your Http\Kernel.php file, go to $routeMiddleware variable and add this line:
'middleware_name' => \App\Http\Middleware\MiddlewareName::class,
And finally, go to your web.php file and set the middleware. An example can be given as:
Route::middleware(['middleware_name'])->group(function () {
Route::prefix('api')->group(function () {
Route::get('/phonefinder', 'SomeController#someMethod');
});
});
Whenever you call api/phonefinder endpoint, it will go to the Middleware first.
What you are looking for is HMVC, where you can send internal route requests, but Laravel doesn't support it.
If you want to have one access point for your modular application then you should declare it like this (for example):
Route::any('api/{module}/{action}', 'InputController#moduleAction');
Then in your moduleAction($module, $action) you can process it accordingly, initialize needed module and call it's action with all attached data. Implement your own Module class the way you need and work from there.
Laravel doesn't support HMVC, you can't have one general route using other internal routes. And if those routes (/find in your case) are not internal and can be publicly accessed then also having one general route makes no sense.
I am using Laravel 5.5 version. I defined my routes in the routes.php file. like this:-
$router->group(['middleware' => 'auth'], function($router) {
$router->resource('/route-name', 'myController#myMethodName');
});
But when I run my application laravel gives error:-
Method [myMethodName#index] does not exist on [App\Http\Controllers\myController].
It is by default put index action after my defined action in the routes.
It is working fine in the laravel 5.3 version. Please solve my problem..
Try this as #devk told in comment:
$router->get('/route-name', 'myController#myMethodName');
By Default resource Request create CRUD requests in routes. For the following methods in Controller.
Index(GET)
Create(GET)
Store(POST)
Show(GET)
edit(GET)
Update(PUT/PATCH)
Delete(DELETE)
You can't pass method name in resource route.
If you want to override any of them. you have to write new one route below the resource Route. Like
Route::get('url','Controller#newMethod');
And Change the method name in Controller with newMethod
For more detail check laravel documents
I'm using the extension laravel-menu in my Laravel application.
This application contains multiple projects with multiple locations attached to each project.
Now I want to define a sidemenu where I can among other manage the locations.
The url of a project is
project/1
The url of the locations page of a project is
project/1/locations
How to setup this side menu in routes.php?
My routes.php code:
Route::resource('project', 'ProjectsController'));
Route::resource('project.locations', 'LocationsController');
Menu::make('sidemenu-project', function($menu) {
$menu->add('Locaties', array('route' => 'project.locations.index','{project?}'))->data('id',1); // this is not working
});
This is outputting the url /project/%7Bproject%7D/locations
Go to your terminal (Command Prompt) and run following command:
> php artisan routes
Then you'll see all the declared routes with their URL and corresponding route name and method name.
I'm very new to Laravel but the Routes page of documentation mentions you create a controller with parameters like this:
Route::get('user/{id}', function($id) { ... });
could you therefore define your route as
Route::get('project/{id}/locations', function($id) { ... });
I think you have this issue due to misconfiguring the routes. To achieve the route structure that you want, you should put your project/1/locations route definition above the first one. Consider your routes.php to be:
Route::resource('project/{project}/locations', ['as'=>'project.locations', 'uses'=> 'LocationsController']);
Route::resource('project', 'ProjectsController'));
I basically have to implement API commands for an external app using their URI pattern. I want to implement all the methods in a Controller so I added the following in routes.php:
Route::controller('/ch', 'CHController');
I have to implement GET /ch/api_function/param1/param2. The issue is the external API's URI uses the '_' syntax for their action and laravel can't route it correctly to the right function.
The Laravel documentation for controllers here http://laravel.com/docs/controllers#resource-controllers suggests
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our UserController would respond to the users/admin-profile URI:
public function getAdminProfile() {}
Just define the routes manually in your routes file
Route::group(['prefix' => 'ch'], function()
{
Route::post('/api_function/{param1}/{param2}', ['uses' => 'CHController#function1']);
Route::post('/another_function/{param1}', ['uses' => 'CHController#function2']);
}