I am working on a Laravel project, where I want to toggle partials based on the URL data like
foo.bar.com/#itemone/create
foo.bar.com/#itemone/view
Basically, I want to pass whether it is create or view to the partials like this
#include('partials.layouts._core_activity_header',['layout_type' => "create"])
How do I achieve this? Any help would be appreciated.
you can use the route name
Route::getCurrentRoute()->getName()
this will return the route alias
Related
I'm working on ecommerce website, Stuck in nav bar, I have created routes something like this:
Route::get('/category/{slug}', 'Site\CategoryController#show')->name('category.show');
In slug i pass slug of Product,
but i want to change something like this
Route::get('/{slug}/{slug}', 'Site\CategoryController#show')->name('category.show');
i want to remove category prefix and pass main category slug as a first parameter and sub category if it exist as a second parameter otherwise it will be empty.
One more thing i am using TypiCMS, for creating Nestable menu and it is working will i have to modify that also to work with the dynamic route.
Sorry I don't know anything about your cms framework but in Laravel you cant use the same name for two bindings in your route, they must each be unique.
Route::get('/{categoyrySlug}/{subcategorySlug}', 'Site\CategoryController#show')->name('category.show');
And in Site\CategoryController you should be able to use :
public function show($categorySlug, $subcategorySlug){
...
}
and then handle them accodingly.
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.
i have some problem about routes in laravel
Route::get('aset/create', 'TransaksiController#aset_create');
Route::get('aset/{id}', 'TransaksiController#aset_view');
these routes was fine, but when i switch the position like
Route::get('aset/{id}', 'TransaksiController#aset_view');
Route::get('aset/create', 'TransaksiController#aset_create');
the aset/create was went to aset/view
whats happening?
thanks!
The aset/create will trigger the Route::get('aset/{id}', 'TransaksiController#aset_view'); as you can see the template displayed is for view aset-view.blade.php not the template for the create, so laravel identifies create as the id param so your db query will return no rows creating the error you see,
To fix this problem you either keep the original order or you change the view route to match only number(or not match create)
Route::get('aset/{id}', 'TransaksiController#aset_view')->where('id', '[0-9]+');
aset-view.blade.php file, that's where you have issue. You are either accessing a variable as object, it could be something null or array which you are accessing as an object
{id} is a parameter for passing it is receving now create as parameter.since it is in top.Rearrange will solve the problem.id will catch anything you pass
I'm trying to create a new page in Laravel and I'm not sure what to do in the context of the Laravel framework.
If it was just html, then you just create a new html file. In Laravel, what are all the things you need to do when creating a new page?
The easiest way in Laravel is to define a route closure, and return a view from it.
In your routes/web.php file:
Route::get('/my-page', function () {
return view('my-page');
});
Then in resources/views you create a file called my-page.php, or if you want to use Laravel's Blade syntax (you probably do) call it my-page.blade.php.
Edit: there's now an even easier way to to it. Also in routes/web.php:
Route::view('/my-page', 'my-page');
This will do exactly the same thing as the previous example, without the need for a closure and explicitly calling view().
There are two locations to add New Page to your Laravel project:
You have to create an additional route in YOURAPP>routes>web.php file.
You have to add PHP file with that name to YOURAPP>resources>views folder. If you want to use BLADE for your project, than you should put name_of_page.blade.php. And you should concider it as a must at the very beginning :).
Now, in routes you should be able to add only first part of the file rather than .blade.php. For example: about.blade.php, you can put in route only about
It should work out of the box :).
You can also try this in routes/web.php
Route::get('/my-page', function () {
return view('my-page');
})->name('my-page');
I Have a add multiple default controllers in Codeigniter site.
I am unable to do that.
Basically I have to remove controller names from url without .htaccess file.
I want to make that dynamic.
I have checked that only default controller name can be removed thats why I want to make dynamic multiple default controllers.
Anyone can help me on this issue.
Thanks
You will have to write the changes to the application/config/routes.php file to do this.
So, if you have a controller called Secondary and it had methods like view, add, search
you would add:
$route['view'] = 'secondary/view';
$route['add'] = 'secondary/add';
$route['search'] = 'secondary/search';
NB that if you're passing params to your methods you will need to add entries for these as will, for example if you wanted to pass something to view() you would need to:
$route['view'] = 'secondary/view';
$route['view/(:any)'] = 'secondary/view/$1';
Hope this helps!