using Laravel 7 Api routes - php

I'm trying to use simple laravel api for getting and sending requests, after define this api routes in api.php:
Route::prefix('Api/v1')->group(function () {
Route::any('login', 'Api\v1\AuthController#login');
Route::any('register', 'Api\v1\AuthController#register');
});
and creating AuthController in app/http/controller/Api/v1 directory:
class AuthController extends Controller
{
public function login()
{
dd(request()->all());
}
public function register()
{
dd(request()->all());
}
}
i get 404 error on this link:
http://127.0.0.1:8000/Api/v1/login
how can i resolve this problem?

Routes in api.php are automatically prefixed with /api. Currently, your routes are:
http://127.0.0.1:8000/api/Api/v1/login
http://127.0.0.1:8000/api/Api/v1/register
So navigating to http://127.0.0.1:8000/Api/v1/login is a 404.
If you remove /Api, and just use Route::prefix('/v1') ... then you should have no issue.
Also, always double check your routes with php artisan route:list to see what's wrong.

The API Routes are already prefixed by /api . I think the correct structure you'd looking for would be
Route::prefix('v1')->group(function () {
Route::any('login', 'AuthController#login');
Route::any('register', 'AuthController#register');
});
This way, you're calling the methods Login and Register from you /Controllers/AuthController file with the route
http://127.0.0.1:8000/api/v1/login

You can use many ways to define routes for API in laraval > routes > api.php file.
In this i'm going to explain how we can use routes group in the laraval..
Route::group([
'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
], function ($router) {
// add and delete customer groups
Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/ this is called to index method in CustomerController.php
Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
});
You can create controller using artisan command with default methods
php artisan make:controller Customers/CustomerController --resource

Related

Attribute [livewire] does not exist

I am having trouble to run my code on Laravel 8 routing with laravel-livewire.
The class is within Livewire\LandingPage.
The error I'm getting is
Attribute [livewire] does not exist
Here are my routes
<?php
use Illuminate\Support\Facades\Route;
Route::livewire('/' , 'LandingPage');
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
If you are using a recent install of Laravel 8, you will have Livewire V2. In this version, Route::livewire()has been removed. Instead, you specify a normal get() route, with the action being the Livewire component class.
Route::get('/' , App\Http\Livewire\LandingPage::class);
If you use livewire v1.x please use this annotation :
//(livewire v1.x)
Route::livewire('/post', 'LandingPage');
If you are using livewire v2.0 please use this one :
//(livewire v2.x)
Route::get('/post', \App\Http\Livewire\LandingPage::class);
From the error it appears you do not have the auth system setup:
Route::group(['middleware' => 'auth'], function () {
// Only with LiveWire v1
//Route::livewire('/blog', 'blog')->section('blog');
// For LiveWire 2.
Route::get('/blog' , 'BlogController#index');
});
You are calling the auth middleware and the error is stating there is no LoginController presently located in Auth\LoginController
Do you have any auth scaffolding setup?
Didn't realize this was such an old thread.
With Laravel 8.29 and LiveWire 2.4.0
UnexpectedValueException
Invalid route action: [App\Http\Controllers\App\Http\Livewire\Blog].
I think that is better tha you need create a new Controller in App\Http\Controllers and link the route with this Controller. In the view use #liveware to your LiveWire Controller.
Route::group(['middleware' => 'auth'], function () {
// Only with LiveWire v1
//Route::livewire('/blog', 'blog')->section('blog');
// For LiveWire 2.
Route::get('/blog' , 'BlogController#index');
});
App\Http\Controllers\BlogController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BlogController extends Controller
{
public function index(){
return view('blog.index');
}
}
resources/views/blog/index.blade.php
#livewire('blog')
Note:
With the fix (https://laravel-livewire.com/docs/2.x/upgrading)
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace) // Remove me
->group(base_path('routes/web.php'));
}
you will have problems with routes in Middlewares
Illuminate\Contracts\Container\BindingResolutionException Target class
[Auth\LoginController] does not exist.

root route problem in laravel 6.2 RouteServiceProvider

i have defined these two different routes in laravel RouteServiceProvider like this:
protected function mapABCRoutes()
{
Route::prefix('abc')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/abc.php'));
}
protected function mapXYZRoutes()
{
Route::prefix('xyz')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/xyz.php'));
}
and i defined a route in abc.php
Route::get('/', function(){ return '<h1>ABC Admin</h1>'; })->name('abc.dashboard');
all defined routes in abc.php are working as well except route('abc.dashboard'). it throws a 404 with message "The requested resource /abc was not found on this server."
same thing resulting for xyz.php im working with all of this things in an ubuntu using laravel 6.2 in apache with mod rewrite enabled. i cant understand why these routes are not working? but the same type route works as well on laravels default route in web.php
Route::get('/', function () { return view('auth.login'); });
Route groups doesn't mean you can override the similar routes multiple time, it usually helps to clean the routes files. For instance, I've created separate route files for some of my main modules and put into their respective route files and map in RouteServiceProvider.
As you're using web routes here, What you can do here to prefix routes likeso,
for xyz.php
Route::group( [
'prefix' => 'xyz'],
function ( Router $api ) {
//your routes
});
and similar could be done for abc.php and so on.
Not Sure... it's may help you...
Route::group(['prefix' => 'abc'], function(){
Route::get('/', function(){ return '<h1>ABC Admin</h1>'; })->name('abc.dashboard');
});

Laravel Routing Route::resource inside Route::group

I am new in Laravel.
routes/api.php I have written this function
Route::group(['namespace' => "Catalogue"],function(){
Route::resource('product','Product');
});
I have created a resource controller:
app/Controllers/Catalogue/Product.php
This is my index method:
public function index()
{
$pdo = DB::select('select count(*) from offers');
return $pdo;
}
I am trying to get the result from index method from url:
http://localhost:8000/api/Catalogue/product
However, this results in 404 not found.
Note: There is no issue in this part of url http://localhost:8000/api
Based on your route the link generated is http://localhost:8000/api/product
If you need the link to be http://localhost:8000/api/Catalogue/product , then add the prefix to the group.
Route::group(['prefix' => 'Catalogue', 'namespace' => 'Catalogue'], function() {
Route::resource('product', 'Product');
});
The namespace only sets the default namespace for the controller. The prefix sets the route prefix for all the routes in the group.
You are hitting the wrong uri.
Check http://localhost:8000/api/product
The namespace in the group route means you are assigning a namespace to a group of controllers. As you can see here https://laravel.com/docs/5.4/routing#route-group-namespaces. It has nothing to do with the routes.
Here you can see the other routes when you make them in the controller.
https://laravel.com/docs/5.4/controllers#controllers-and-namespaces

Check the user's auth in all controllers' actions

I use Laravel 5.3 and I have the following problem.
[UPDATE]
My initial trouble was the appearance of an error when performing actions on the site when the user was not logged in the system.
This happened when the browser is started, where cached information is displayed by default on the page. Site interface displayed for logged users, and in his system was not. At the same time, producing some action, I get an error that the user is not authorized.
I also have group auth middleware for all my routes. When I reboot page of the site, the middleware is activated and redirectedme to the login page. The main problem is the browser shows the cached information.
So, in addition to middleware for routes I decided to make auth check in controllers.
[/UPDATE]
I want to check user's auth in every controller's action. Making the auth check in every controllers' action manually isn't a solution, because there are many controllers and actions.
So I decided to make it globally.
As all controllers extends Main Controller (App\Http\Controllers\Controller.php), I decided write the
auth()->check() in constructor:
function __construct()
{
if(auth()->check()) dd('success');
}
But... nothing happened((( Then I found the callAction method in BaseController which Main Controller extends and made checking here:
public function callAction($method, $parameters)
{
if(auth()->check()) dd('success');
return call_user_func_array([$this, $method], $parameters);
}
This time everything's OK, but I don't like this solution, because editing the core files isn't good.
Finally, I redeclared callAction method in Main Controller with auth checking, but I don't like this way too.
Is any solution?
You should use middleware:
Route::get('profile', ['middleware' => 'auth', 'uses' => 'UserController#showProfile']);
Or:
Route::get('profile', 'UserController#show')->middleware('auth');
Or using middleware groups:
Route::group(['middleware' => ['auth']], function () {
// Controllers here.
});
Or using controller's construct:
public function __construct()
{
$this->middleware('auth');
}
You can use auth middleware in your controller
public function __construct()
{
$this->middleware('auth');
}
check here : https://laravel.com/docs/5.3/authentication
if there is a group of routes this would be the easiest way
Route::group(['middleware' => ['auth']], function()
{
// here all of the routes that requires auth to be checked like this
Route::resource('user','UsersController');
}
another ways
function __construct()
{
$this->middleware('auth');
}
another way is specified on controller routes
Route::get('profile', [
'middleware' => 'auth',
'uses' => 'UserController#showProfile'
]);
see documentation
https://laravel.com/docs/5.0/controllers#controller-middleware

How to use wild card in admin routing in laravel 5

I am using code that is below for admin routing in laravel.
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'Admin\AdminController#home');
Route::get('/users/userList', 'Admin\UserController#userList');
Route::get('/users/detail', 'Admin\UserController#detail');
Route::get('/posts/view', 'Admin\PostController#view');
Route::get('/posts/edit', 'Admin\PostController#edit');
Route::get('/posts/add', 'Admin\PostController#add');
});
This is working fine for me. But when I add new functions in code for that I have to write routing in routes file. For example: If I want to add edit functionality in users controller, for that I have to add new route like .
Route::get('/users/edit', 'Admin\UserController#edit');
So I have to add routing for each function.
I want to know How to use wild card for admin routing so that I have to write routing only for controller not for each function for example.
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'Admin\AdminController#home');
Route::get('/users/:any', 'Admin\UserController#:any');
Route::get('/posts/:any', 'Admin\PostsController#:any');
});
wild card replace the function name, and auto ridirect to that function.
You could use implicit controllers that will do what you need.
First declare a route for your implicit controller
Route::controller('users', 'UserController');
Then, on your controller, you have to follow a convention for naming your routes with HTTP verbs used to access them (get for GET, post for POST, any for both)
class UserController extends Controller {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
A note about composed method name from documentation
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() {}

Categories