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');
});
Related
I have three routing files inside routes folder:
web.php
api.php
admin.php
I've registered the file admin.php inside boot() method of RouteServiceProvider as following:
public function boot() {
$this->configureRateLimiting();
$this->routes(function () {
Route::domain("admin." . env("APP_URL"))
->namespace($this->namespace)
->group(base_path("routes/admin.php"));
Route::domain("api." . env("APP_URL"))
->middleware("api")
->namespace($this->namespace)
->group(base_path("routes/api.php"));
Route::middleware("web")
->namespace($this->namespace)
->group(base_path("routes/web.php"));
});
}
Let's say i've defined the following route in web.php:
Route::get("test", function() {
return "Hello from website route";
});
If i try to access this route using mywebsite.com/test it acts like expected.
However, if i try to access the link admin.mywebsite.com/test it would still some how falls back to mywebsite.com/test and give the exact same output.
What's the problem here ?
Because web.php is not restricted to a domain. As a consequence, the routes defined there are accessed by every domain that targets your laravel application.
If you only want the root domain to access the routes defined in your web.php, you could specify it in the RouteServiceProvider:
Route::middleware("web")
->domain(env("APP_URL")) // Add this line
->namespace($this->namespace)
->group(base_path("routes/web.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
I got confuse and I got an error, why does my routing process not work, The error gives me Route [index] not defined, but on other hand I already defined the index to HomeController, take a look at my process that I did,
Note: I used laravel version: 5.8*
I create a index.blade.php
Add the routes to the web.php and I used this code
`Route::get('/index', 'HomeController#index');
I add the public function index to the HomeController
Web.php
Route::get('/index', 'HomeController#index');
HomeController
public function index()
{
return view('index');
}
My URL:
Error:
The problem might be in your index view.
Looks like you are trying to access route using route name and you have not defined the route name for index route.
So in web.php add ->name('index')
Route::get('/index', 'HomeController#index')->name('index');
You have to provide name of the route in your routes.
Route::get('/index', 'HomeController#index')->name('index');
You can also use the below syntax
Route::get('/index', [
'as' => 'index',
'uses' => 'HomeController#index'
]);
for more information please have a look at the docs
https://laravel.com/docs/5.7/routing#named-routes
Try This
if you use route this way
Route::get('/index', 'HomeController#index');
//then your url will be
URL/index
OR use this way
Route::get('/', 'HomeController#index');
//then your url will be
URL
Try with localhost/folder_name/public/index if this works for you then probably problem is with virtual host creation.
Somewhere in your view you are using {{route('index')}}.
Add ->name('index') in the end of your route.
Route::get('/index', 'HomeController#index')->name('index');
Hope this will help.
I am not sure the best way to do this but basically I have some routes set up and some vendor ones as well but my kind of "catch all" route is getting called when I need the vendor "/forum" to be used.
Here are my routes:
Route::get('/', function () { return view('welcome'); });
Route::get('/contact', function () { return view('contact'); });
Route::get('/login', function () { return view('login'); });
Route::get('/signup', 'UserController#create');
Route::get('/logout', 'UserController#logout');
Route::get('/{slug}', 'PageController#show');
You can see the last route basically just gets the slug and and then in the controller I return the page by slug. The issue is with /forum the PageController#show is getting called since I assume Laravel looks at this route file before the vendor. Is there a better way of setting it up so Route::get('/{slug}', 'PageController#show'); gets called as the last possible option after vendor routes as well?
Laravel routes are loaded in order they are defined so the only way to have your /forum match before /{slug} is to make sure that route is loaded first. To guarantee it is loaded last I would suggest adding it after all the other routes are loaded in app/Providers/RouteServiceProvider.php which would look like:
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
Route::get('/{slug}', 'App\Http\Controllers\PageController#show');
}
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel