So currently in my Laravel app I've got the route;
Route::get('/{any}', 'SinglePageController#index')->where('any', '.*');
And in this state, it works. However I want to add a prefix to it such as /app/{any} and for it to be inclusive of app as well. So /app would still go to SinglePageController.
I've tried doing something like that and it works for everything except the /app route itself.
Hopefully someone can provide some suggestions.
Try this:
Route::group(["prefix" => "app"], function(){
Route::get('/{any?}', 'SinglePageController#index');
}
I converted the any parameter to optional, so it can go to SinglePageController#index when it's not given too.
Also you may define the parameter of the index method as optional too. Like:
public function index($any = null){
Related
I am building an application where users are registered and want to be redirected to their individual dashboard like this .
http://localhost/project/{username}/dashboard,
Now, its happening like
localhost/project/vendors/dashboard (here all users are accessing same URL)
but I want to make it like :
http://localhost/project/{username1}/dashboard, http://localhost/project/{username2}/dashboard
Googled lot but none of them are explained well and working.
Please assist with complete flow.
I want to declare the value of {username} globally and use it in route as prefix.
I dont want to use it before each name route. will use it as prefix and group with all vendors routes
I have made this, and its working as
localhost/project/vendors/dashboard
Route::prefix('vendors')->group(function () { Route::middleware(['auth:vendor'])->group(function () { Route::get('/dashboard', [VendorController::class, 'dashboard'])->name('vendor.dashboard'); });
});
You can specify route parameters in brackets like so {parameter}, change your code into this.
Route::get('project/{username}/dashboard', [UserDashboardController::class, 'dashboard'])
->name('user.dashboard');
In your controller you could access it like this.
class UserDashboardController
{
public function dashboard(string $username)
{
User::where('username', $username)->firstOrFail();
// something else
}
}
Seems like in your routes your are mixing vendor prefix logic in with something that in your specifications of what your urls should look like does not match. Which i think is making up some of the confusion on this case.
You can use route prefix like this
Route::prefix('{username}')->group(function () {
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [UserController::class, 'dashboard'])->name('user.dashboard');
});
});
I have a group definition inside api.php.
I wonder why the first controller works fine
but the second would return Target class [UserExpertController] does not exist.
I like the second syntax more as im used to it from writing web.php routes.
any idea?!
Route::name('experts.')->prefix('experts')->group(function () {
// returns all experts
Route::get('/',[UserExpertController::class, 'index'])->name('index');
//or
Route::get('/','UserExpertController#index')->name('index');
actually this works
Route::get('/',[UserExpertController::class, 'index'])->name('index');
this doesn't work
Route::get('/','UserExpertController#index')->name('index');
I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];
you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');
If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs
After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.
here is the URL i want to access an articel in Laravel.
http://mysite.test/art-entertainment-articles/poetry-articles/guide-praising-comments-1.html
now article_slug is "/art-entertainment-articles/poetry-articles/guide-praising-comments-1.html".
i made a route like this.
Route::get('/{any:.*}', 'ArticlesController#article');
but it is showing error 404 not found. now i want to get article by matching slug like this.
$article = Article::where('article_slug', '=', $article_slug)->first();
what should i write in route? it breaks at slashes and count not read the method.
You are probably better off using the fallback function like so
Route::fallback(function () {
//
});
This will catch all routes that are not defined above it. Then you can add the logic to hit your controller and figure out the article you require from the url.
Working on a Laravel 4.2 project. What I am trying to accomplish is pass every URI pattern to a controller that I can then go to the database and see if I need to redirect this URL (I know I can do this simple in PHP and do not need to go through Laravel, but just trying to use this as a learning experience.)
So what I have at the moment is this:
Route::group(array('domain' => 'sub.domain.com'), function()
{
Route::get('?', 'RedirectController#index');
});
I am routing any subdomain which I deem as a "redirect subdomain" ... The ? is where I am having the problem. From what I have read you should be able to use "*" for anything but that does not seem to be working. Anyone have a clue how to pass any URL to a controller?
And on top of that I would ideally like to pass the FULL URL so i can easily just check the DB and redirect so:
$url = URL::full();
Try this:
Route::group(array('domain' => 'sub.domain.com'), function()
{
Route::get('{path}', 'RedirectController#index')
->where('path', '.*');
});
And your controller will reseive the path as first argument
public function index($path){
// ...
}
In case you're wondering, the where is needed because without it {path} will only match the path until the first /. This way all characters, even /, are allowed as route parameter