iam new in laravel , and i wrote this code at routes/api.php in laravel 9
Route::group([
'prefix' => 'auth',
'namespace' => 'Auth'
], function(){
Route::post('register', 'RegisterController');
});
and then i got cant run php artisan serve , it said
UnexpectedValueException
Invalid route action: [Auth\RegisterController].
at G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:92
88▕ */
89▕ protected static function makeInvokable($action)
90▕ {
91▕ if (! method_exists($action, '__invoke')) {
➜ 92▕ throw new UnexpectedValueException("Invalid route action: [{$action}].");
93▕ }
94▕
95▕ return $action.'#__invoke';
96▕ }
1 G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:47
Illuminate\Routing\RouteAction::makeInvokable("Auth\RegisterController")
2 G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\Route.php:190
Illuminate\Routing\RouteAction::parse("api/auth/register", ["Auth\RegisterController", "Auth\RegisterController"])
someone please help me :)
Add RegisterController function
Route::group([
'prefix' => 'auth',
'namespace' => 'Auth'
], function(){
Route::post('register', 'RegisterController#store');
});
You are missing a parameter in your post function from Route.
You want something like
Route::post('route_name', 'Controller#myFunction')
Or in your case:
Route::post('register', 'RegisterController#registerFunctionName');
Other variation per 9.x documentation:
Route::post('register', [RegisterController::class, 'registerFunctionName']);
Please refer to:
https://laravel.com/docs/9.x/routing
This is an invokable controller yes?
you need to just alter the syntax
Route::group([
'prefix' => 'auth',
'namespace' => 'Auth'
], function(){
Route::post('register', [RegisterController::class]);
});
and then import the class at the top of your routes file and make sure you have a single public method of __invoke() in your controller.
Related
I've run into a problem with the updated version of Laravel and the new routing.
The route with the resources works just fine and uses the correct namespace, the problem is with the direct route "users/table", it's not using any namespace and returns "Target class [UserController] does not exist."
.
When I apply the full controller namespace and class name it works. I've modified my RouteResourceProvider.php and it's loading the default namespace on boot.
My question is why the resources method works but for the custom route, I have to specify the entire namespace inside the route group with an already set namespace?
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'Admin'], function () {
Route::post('users/table', [UserController::class, 'table']);
...
Route::resources([
'users' => UserController::class,
...
]);
});
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'App\Http\Controllers\Admin'], function () {
Route::post('users/table', [UserController::class, 'table']);
...
Route::resources([
'users' => UserController::class,
...
]);});
I have created ApiController in App\Http\Controllers\Api\v1
Also created auth using laravel/ui
Default created function for front end working perfectly.
But issue is when try to call the ApiController
My API Route file is as below
Route::group(['prefix' => 'api/v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController#register');
});
And my API controller look like
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function register(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
}
Before 404 it was csrf error and i have resolving it by
protected $except = [
'/register',
];
in Http\Middleware\VerifyCsrfToken
I cant figure out two question
How to except my entire api call from CSRF using $except..
How to solve 404 for register method , i use postman with POST request and call URL http://localhost/larablog/api/v1/register
Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class.
1) 404 error :- Remove api from prefix route.
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController#register');
});
http://localhost/larablog/api/v1/register
1. If you are using a route group:
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController#register');
});
Your $except array looks like:
protected $except = ['v1/register'];
2. If you want to exclude all routes under v1
Your $except array looks like:
protected $except = ['v1/*'];
i just want to group all my admin routes in my laravel. I'm a beginner in laravel and i want to synchronize all my admin routes in one group, my question is, why i cant put the post route inside the group of my admin routes?
Here is my routes:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
});
my above code was returning error , where laravel says admin.login route doesn't exist. Then i tried to put the post route outside the group and it works. Why?.
Here is the code where returns no error:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
});
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
Because you use as in your route group and it's admin:: and you may link to admin.
Now it goes to admin::login and you need admin.login
I have the following routes in place:
Route::group(['prefix' => 'api/v1', 'middleware' => 'api'], function() {
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController#authenticate');
Route::resource('users', 'UserController');
});
The UserController has a test to ensure that when a user is submitted via POST, that it validates the input correctly. This should return a 422 when invalid, but it actually returns a 302. In Postman, it raises a CSRF token error, suggesting the web middleware group is being applied, which is not the behaviour I want.
How can I prevent this happening?
In RouteServiceProvider.php change
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
to:
$router->group([
'namespace' => $this->namespace,
], function ($router) {
require app_path('Http/routes.php');
});
And then wrap your web routes with Route::group(['middleware' => 'web']) in routes.php. So api routes will be not affected by web middleware.
Thanks for any help you can provide. I am 10 days into learning Laravel 5.1 so any suggestions on what I have missed will be greatly appreciated!
I am having an issue with the resolution of named routes in Laravel 5.1. I am building an app that will have a URL in the format of {organisation}.website.com, where {organisation} is defined at registration of customer.
The code routes perfectly when using sample subdomains, so long as I hardcode the route address (e.g.: redirect('/home');), but when I try and route by named routes from Controller (e.g.: redirect()->route('session.create');) the routes resolve like this:
http://%7Borganisation%7D.website.com/home
My routes look like this:
<?php
/**
* Entity routes - resolves {organisation}.website.com
*/
Route::group([
'domain' => '{organisation}.' . env('APP_DOMAIN')
], function(){
/*
|----------------------------------------------------------------------
| Freely available routes for login, registration, password reset etc
|----------------------------------------------------------------------
*/
Route::group([
'middleware' => 'guest'
], function(){
// Login
Route::get('login', ['uses' => 'SessionController#create', 'as' => 'session.create']);
Route::post('login', ['uses' => 'SessionController#store', 'as' => 'session.check']);
});
Route::group([
'namespace' => 'Website',
'middleware' => ['authorise'],
], function(){
});
/*
|-----------------------------------------------------------------------
| Potentially secured routes
|-----------------------------------------------------------------------
*/
Route::group([
'middleware' => ['authorise']
], function(){
// Logout and destroy all Auth data
Route::get('logout', ['uses' => 'SessionController#destroy', 'as' => 'session.destroy']);
});
});
In my Controllers I call the routing like this:
return redirect()
->route('session.create')
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
and it successfully completes, but with the above URL and 404. If I change to this it works perfectly.
return redirect('/home')
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
What have I missed in my organisation sub domain set up to make named routes work? Thanks!
Got it working. Below is how I fixed the issue, which essentially I had to bind on the fly to the route the slug of the sub-domain. Hope this helps someone!
I first had to pass the Organisation class to my controller methods and change the route calls like this:
public function destroy(Organisation $organisation)
{
Auth::logout();
return redirect()->route('session.create', ['organisation' => $organisation->slug]);
}