issue in laravel hierarchy dynamic pages - laravel

i want to make a site that maybe has 3-4 levels or more for its pages
like:
example.com/level1/level2/level3 /.../level n
but when i use :
Route::get(/{page?slug} ,[controller::show]})
it returns 404 because "get" function in Route facade return first slug instead of full url so my controller can find any data in DB
how can i manage it?
(all pages is created dynamically from my own cms and will be readed from DB)
ofcourse i did it for 3 multilevel like
Route::get(/{page?}/{page2?}/{page3?} ,[controller::show]})
public function show(page =null , page2 =null, page3 =null){
if(!is_null(page3)){
...
}
..
but it is a messy code and is not proper for more level

Related

Laravel mix static with dynamic slug in a base route

I have a question and I don't find a solution, I'm building a landing page section and in this section I will have some pages with identical layout, and content will be provider by db, some other pages will return a blade view.
How can I manager the route?
Url structure will be domani.com/landing/{slug}
Which is the best way to compare the {slug} with provider static slug and if not find search in the db?
You can use in your route a where condition. Like that?
Route::get('/landing/{page}', App\Http\Controllers\LandingPageController::class)
->name('page')
->where('page', 'about-us|imprint|contact');
In this example only /landingpage/about-us, /landingpage/imprint, /landingpage/contact requestable.
And in your LandingPageController you can add a method with a switch case to redirect to the right page.
class LandingPageController {
public function __invoke($page)
{
return view('pages.'.$page);
}
}

Page you are looking for not found laravel 5.5

I am getting the
Sorry, the page you are looking for could not be found.
On Laravel 5.5. I am sure I'm missing something very small. But not sure what it is because I'm a laravel learner. Please see below:
ROUTE
Auth::routes();
Route::get('/curriculum-sections','CurriculumsectionsController#index')->name('curriculum-sections');
Route::resource('/curriculum-sections','CurriculumsectionsController');
CONTROLLER
public function show(Curriculumsection $curriculumsection)
{
//
$curriculum = Curriculum::findOrFail($curriculumsection->id);
return view('curriculum-sections.show', ['curriculum'=>$curriculum]);
}
and I also made sure that the page exists in the views folder. While troubleshooting I also did php artisan route:list and this is what I got
Edit:
I am accessing the error from:
http://localhost:8000/curriculum-sections/1
The problem is that your route defines id for model Curriculumsection. You're using model binding, which will automatically query for Curriculumsection::findOrFail(route_id) (in your case route_id is 1). And then you're using the same id to query model Curriculum as well, with ->findOrFail(route_id).
So in order for this route to return anything other than 404, you have to have a record of Curriculumsection with id 1 and a record of Curriculum with id 1 in your database.
I'm not sure how your database is set up, or how these 2 models are related to each other, but definitely not by same id (otherwise, why not have all data in the same table).
Something like this would make more sense (binding the Curriculum model directly):
public function show(Curriculum $curriculum)
{
return view('curriculum-sections.show', ['curriculum'=>$curriculum]);
}
This would bind the Curriculum model to the route and automatically fetch the one with passed in id.
Or something like this for your use case, but it assumes that you have a working relationship called curriculum() on your Curriculumsection model:
public function show(Curriculumsection $curriculumsection)
{
$curriculum = $curriculumsection->curriculum;
return view('curriculum-sections.show', ['curriculum'=>$curriculum]);
}
I dont think that you need the first get route. You just define the Route::resource(...) and that's it. Laravel handles the different requests.
See here -> Resource Controllers

How to get pretty URL in Codeigniter?

I have a product controller (Codeigniter) where I load 40 categories in my index function. When I click on a category, I want to load all item of that particular category. To do that, I can easily write a function to load all that items.
Function load_item($categoryId’)
{
// in short
…… where categoryId = ‘$categoryId’
// then load it to view
}
Then I have URL like /product/load_item. But I want URL like /product/laptop or /product/desktop (/product/category_name). So, it's not possible to write 40s function for every category and also its not optimal solution. I don’t want to change anything in index function. Have you any idea please???
You have to setup the routes for the url's so under your config folder go to routes and then create a route like so
$route['product/(:any)'] = 'catalog/product_lookup';
You can find all relevant information in the Codeiginter User Guide
Method 01
In routes.php
$route['category/(:any)'] = 'category/load_item';
Output - www.example.com/category/(value/number-of-category)
Method 02
In View URL passing method(Means <a>)
Click me
so in controller
Function item($catgoryName, $categoryId)
{
// in short
where categoryId = '$categoryId';
// then load it to view
}
Output - www.example.com/category/item/laptops/1

Dynamic Controllers in CodeIgniter

I am in the process of creating a new website which loads all master and child categories from the database. I have tested the navigation as well, i.e., if I click any master category, it perfectly loads all the respective child categories without any issue. However, at present, I am doing this by passing query string in the URL. For instance
http://localhost/MyController?id=32145
Let's assume that the id, 32145, represents a master category namely 'About us'. My question is how can I change the above URL to something like:
http://localhost/Aboutus
and if there is any child category under About us than it should display as:
http://localhost/Aboutus/Mission
Please help me out as I am really stuck with this problem.
by default CodeIgniter uses a segment-based approach, you can do URL routing in way like your second part of the question - "and if there is any child category under About us"
$route['product/(:any)'] = "catalog/product_lookup";
more here: https://ellislab.com/codeigniter/user-guide/general/routing.html
but if you want to rewrite complete URL than you should probably check .htaccess rewriting
It is not easy, do once for migration.
In database you can store the New controller/url for products (if it is not have yet)
Create new Controllers
Route controller which redirect the old Url to the New Url
controllers
Route old urls to Route controller
Route controller something like this:
public function old_url($aProdId) {
if (is_null($aProdId)) {
// error cannot be null
}
$NewUrl = $this->new_url_model->getNewUrl($aProdId);
if (!$NewUrl) {
// error new url not exist
return;
}
redirect(base_url($NewUrl), 'refresh');
}

ASP.NET MVC Routing - Thousands of URL's

My ecommerce application stores URL's for item pages in the database. There are thousands of these URL's, which are all root level (i.e. domain-name.com/{item-page-url}).
If I add all of these URL's to the route table by using a simple for loop to call RouteCollection.MapRoute for each URL site performance degrades exponentially. Why? The reason for this is here.
How should I properly handle this situation? Adding all of the routes to the route table doesn't seem right (not to mention the performance pretty much confirms that). I've seen a few ideas about inspecting all incoming URL's and then trying to match that to the URL's in the database but don't fully understand how I'd implement that, nor am I sure if it's the best approach.
Any ideas or suggestions? This seems like it would be not so uncommon, but I haven't found a concrete way to handle it.
If you can change your route to
mycreativeshop.com/product/my-product-name then adding following route to the top of your route config file can help you.
routes.MapRoute(
"CustomRouteProduct",
"product/{id}",
new { controller = "yourcontrollername", action = "Index" }
);
and in the action map the parameter value with name of your product name
public ActionResult Index(string id)
{
//var prdName = id.Replace("-", " ");
//look up prdName in database
return View();
}
Update
Added following as a top route
routes.MapRoute(
"CustomRouteProductZX",
"{id}",
new { controller = "Content", action = "Index" }
);
and by accessing http://localhost:12025/Car-Paint I was directed to Index action of ContentController where I accessed "Car-Paint" in parameter id
But, above having above blocks patterns like http://localhost:12025/Home/ (here Home is also treated as a product)

Resources