Laravel 5 - Named route with parameter - laravel

In my web.php file, I created two routes :
Route::get('/{name}', 'PublicController#index')->name('welcome');
Route::get('stats', function () { return route('welcome', 'enrique'); });
My controller looks like:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PublicController extends Controller
{
public function index($name)
{
return view('welcome');
}
}
I already set up a virtual host in my local machine which is http://blog.test
When I Call http://blog.test/stats in my browser, it shows me the content of my homepage. But when I reorganize my twho route in web.php file in that way
Route::get('stats', function () { return route('welcome', 'enrique'); });
Route::get('/{name}', 'PublicController#index')->name('welcome');
It works fine.
Can you please explain why it behaves like that? Thanks

What you have is the same route being overwritten. In order to have them both work, you will have to add something before your custom parameter:
/something/{name}
Otherwise stats is assumed to be value for your parameter name

Laravel route goes to first matched routes, so in your case if it sees /stats
Route::get('/{name}', 'PublicController#index')->name('welcome');
It becomes variable $name for PublicController#index
Check more article regarding this

It is happening because , when you add your {parameter} as after / , all routes defined after that, are considered as of that type
Route::get('/{name}', 'PublicController#index')->name('welcome');
// below routes not work
Route::get('stats', function () {});
Route::get('test', function () { });
Route::get('hello', function () {});
same thing happen if you create new route like below :
Route::get('post/{slug}', function () {});
// this get routes are also not work
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
so it is good practice that always define parameterised route at the
last
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
Route::get('post/{slug}', function () {});

Related

Laravel route group with variable prefix and where condition

I would like to create a route group in Laravel with a variable as a prefix. I need to set certain conditions too. How to do it properly?
I was following docs: https://laravel.com/docs/8.x/routing#route-group-prefixes but there are only general examples.
This code should create 2 routes: /{hl}/test-1 and /{hl}/test-2 where {hl} is limited to (en|pl), but it gives an error: "Call to a member function where() on null"
Route::prefix('/{hl}')->group(function ($hl) {
Route::get('/test-1', function () {
return 'OK-1';
});
Route::get('/test-2', function () {
return 'OK-2';
});
})->where('hl','(en|pl)');
The group call doesn't return anything so there is nothing to chain onto. If you make the where call before the call to group, similarly to how you are calling prefix, it will build up these attributes then when you call group it will cascade this onto the routes in the group:
Route::prefix('{hl}')->where(['h1' => '(en|pl)'])->group(function () {
Route::get('test-1', function () {
return 'OK-1';
});
Route::get('test-2', function () {
return 'OK-2';
});
});
By analogy with this answer:
Route::group([
'prefix' => '{hl}',
'where' => ['hl' => '(en|pl)']
], function ($hl) {
Route::get('/test-1', function () {
return 'OK-1';
});
Route::get('/test-2', function () {
return 'OK-2';
});
});
Does this solve your problem?

Laravel pass subdomain as a value to another route

now I have this code
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/', function($subdomain)
{
Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index');
});
});
I want to pass {subdomain} to Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index'); for when call subdomain.localhost/identifier I call AController#index I should pass the value of subdomain to AController#index
I think by using Route::input('subdomain'); you can access the subdomain parameter.
Inside AController:
public function index() {
dd(Route::input('subdomain'));
}
Please try this.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
I figured out the answer thanks guys for your help ,
the answer is just call the route inside route::group
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/{identifier}/{reCreate?}','AController#index');
});
and inside Controller function index I can access the value as normal function($subdomain)

Laravel 5.4: How to set a first and default parameter to route() function

I 'm passing a prefix parameter to all routes on my webiste:
Route::prefix("{param}")->group(function () {
# code...
});
Sometimes I need to call the route() function in the views. The problem is there, because I need to pass the $param as first parameter like:
resources\views\welcome.blade.php
About Us
The question is: I do not need to pass $param in route() function because its not necessary. How to avoid this and do just the following:
About Us
Is there a way to create a middleware and set a "global" configuration to route() function?
By using #William Correa approach, I've created a helper function to setting up a prefix parameter to default Laravel function helper route().
app\Helpers\functions.php
function routex($route, $params = [])
{
if (!is_array($params)) {
$params = [$params];
}
// Set the first parameter to App::getLocale()
array_unshift($params, App::getLocale());
return route($route, $params);
}
Now when I try to get a link to route() by name, just use routex('about-us') and the routex() function will put a prefix parameter like App::getLocale() or anything else you want.
In your Controller you can protect your route like this:
public function __construct()
{
$this->middleware('auth');
}
and than
Route::get('/about', 'yourcontroller#yourmethod')->name('name');
or in your web.php you can declare a group without a prefix like this
Route::namespace('name')->group(function () {
Route::get('/about', 'yourcontroller#yourmethod')->name('name');
});
Yeah, like this:
Route::get('about-us', 'AboutController#getAboutUs')->name('about-us');
Now if you call route('about-us') it will generate url to .../about-us.

Laravel redirect after delete item

In my Laravel app, I want to delete an item and redirect immediately after that to the home page. Ideally, I want to do this purely in the routes file:
Route::get('event/{event}/delete', ['as' => 'events_delete', 'uses' => 'EventsController#destroy', function () {
return Redirect::route('events_list');
}] );
In my destroy function I have the following:
public function destroy(Event $event)
{
$event->delete();
}
The delete itself works (after refresh), but the redirect part does not seem to work. How can I redirect to the homepage immediately after the delete happened?
You either use the controller or the closure but not both, I don't think you are allowed to use them both.
But in your controller you can set the redirect like this:
public function destroy(RequestEvent $event)
{
$event->delete();
return redirect()->route('your_home_route');
}

get current path outside of Route

In this URL: http://siteName/html/test
I want to catch test, of course inside the route.
This works great:
Route::get('test', function()
{
return Route::getCurrentRoute()->getPath();
});
But I want to access the path outside of any route and in my route.php file like this:
// routes.php
return Route::getCurrentRoute()->getPath();
Route::get('test', function()
{
...
});
In case you try to catch all routes. Add this as your last route.
Route::any('{all}', function($uri)
{
return Route::getCurrentRoute()->getPath();
})->where('all', '.*');

Resources