Request::is() with variable - laravel

I have route like this
Route::get('/edit_faq/{id}', 'Admin\DashboardController#add_faq');
with prefix
admin
and i want to use Request::is() but didn't find any thing to use with variable .
also i am using this in blade file like this
if("{{Request::is('admin/faq')}}"){}
is there any way to check this in blade like we do in route file just by declaring variable {id}
thanks in advance

You have to provide arguments to it.
#if(Request::is('admin/faq', 'admin/edit_faq/*'))
Then it will match with any of the given arguments. Hope you are looking for this only.

I had a situation like yours where I wanted to add a variable to Request::is() for a list with a foreach loop in a laravel blade template.
class="{{ Request::is('*items/edit/'.$item->id) ? 'active' : '' }}"
Add variable outside ' ' and it will work.

correct way to use in blade file is like this:
#if(Request::is('admin/faq'))
...
#endif
in controller :
if ($request->is('admin/*')) {
//
}
The is method allows you to verify that the incoming request path
matches a given pattern. You may use the * character as a wildcard
when utilizing this method:

Related

How to set custom route name before main route in Laravel?

I want to add username before each route..
ex:
sam/productDashboard
james/productDashboard
note - Username is getting from session.
i tried like this. it doesn't work
Route::get( session()->get('name').'/productDashboard',[ProductController::class,'ProductDashboard'])->name('productDashboard');
This is not the way to use variable inside a route.
Do it like this:
Route::get('{username}/productDashboard',[ProductController::class,'ProductDashboard'])->name('productDashboard');
and when you are referencing to this route with a link do it this way:
<a href="{{route('productDashboard',['username' => session()->get('name')])}}">Link</>
it registered on the start you can't do in this way
You could set it like params
Route::get('{username}/productDashboard',[ProductController::class,'ProductDashboard'])->name('productDashboard');

how to pass static value through url in blade file?

I want to send static value through my url from my blade file .But this not working.can you give me the solution
>
> This is link
you have syntax error you can fix by
This is link
here ?rfq=1 this will come under '' this tag as it is string
as your router is defined as this
Route::get('/user-login/{email}/{password}', 'UserController#user_login')->name('users.user_login');
you should use route name and pass the data like this
This is link
then if you need to pass you can use like this
This is link
this will generate url like localhost:8000/user-login/example#mail.com/1234?rfq=1"
ref link https://laravel.com/docs/8.x/helpers#method-route
If you are using {} in your route(route parameter method) then no need to have '?' in your URL. If you need 127.0.0.1:8000/user-login?rfq=1 as your URL then create route without route parameters as
Route::get('/user-login,'UserController#user_login')->name('users.user_login');

What is the difference between those routes?

I define route for update profile logic, when I used first logic It does not work but the use of second logic works fine. So I don't know what is the difference between those.
1. Route::post('/profile', 'ProfileController#update');
2. Route::post('/profile', 'ProfileController#update')->name('profile');
The only difference between them, the name,
so If you put in form action something like {{ route('profile') }} you mean: go to route that has name profile.
Read this for more details.
Routes with name eg Route::post('/profile', 'ProfileController#update')->name('profile');
can be accessed in blade using {{route('profile')}}
whereas the other one can only be accessed using url(). e.g
{{url('/profile')}}
The second one is a 'named route'. It allows you to reference your route by a name.
Laravel 5.7 Docs - Routing - Named Routes
Well the obvious difference is the added "->name('profile')" named route to your second line. You have tagged this post with laravel-5.7 so I have linked the documentation for this version: https://laravel.com/docs/5.7/routing#named-routes
It appears to me that perhaps you have some logic in the update function of your ProfileController like so:
if ($request->route()->named('profile')) {
//
}
Which would change the outcome of the request. Hope this helps, best regards.

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

codeigniter with same route name with different controller functions

I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
Is it possible to to give same route names with different controller functions.
Then how ?
No, you can't.
If you write same route again, it will overwrite first one.
It is just like an array variable.
If you assign any other value to variable, first one will overwrite.
But you can specify HTTP method with route.
No you can't do like this, you are going to make api then define method that will help
$route["signup"]["post"] = "Controller/signup";
$route["signup"]["GET"] ="Controller2/fbsignup";
I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
then try this in routes and url should be like signup/signup and signup/fbsignup
$route["signup/signup"] = "Controller/signup";
$route["signup/fbsignup"] ="Controller2/fbsignup";
call the url like
signup/signup
signup/fbsignup

Resources