How to create codeigniter routes like wordpress - codeigniter

I have an ecommerce website with wordpress, but now i wanna redevelop in CodeIgniter.
I have hundred indexed url in google.
Here's url format that I expexted with CI routes.
I have tried using this code, but when I try to access blog post it will redirect to city page
$route['(:any)'] = 'product/city/$1/$2';
public function city($name) {
}
result :
www.store.com/kuala-lumpur
public function category($name, $city) {
}
result :
www.store.com/bouquet-kuala-lumpur
public function blog($title) {
}
result :
www.store.com/this-birthday-gift-will-make-your-boyfriend-surprised
City and Category will take from database.
Thanks for your help.

for that purpose you need to save urls in database table (permalinks).
on save/update, get title of record and make it user friendly url and save in permalinks.
That table structure can be:
id
permalink
entity_type (city, category, blog)
entity_id
so based on entity type, it will join on relevant table like city, blog or category and map the urls.
www.store.com/bouquet-kuala-lumpur
www.store.com/kuala-lumpur
www.store.com/this-birthday-gift-will-make-your-boyfriend-surprised

Related

Category name on post

On my posts page, I want to display the category name rather than the ID. I've tried searching and trying every solution I can find here but still struggling, so any help would be massively appreciated.
In my policy resource controller, I have:
public function show(Policy $policy)
{
return view('policies.show',compact('policy'));
}
In my Policy model, I've got.
public function category()
{
return $this->belongsTo(Category::class);
}
I've got cat_id in my policies table, and in my categories table, it is id and category_name. So, of course, I can show the category ID on the policy page by doing the following.
{{ $policy->cat_id }}
But I need to convert that into the category name. I know this is a basic question, but I've only recently started getting into Laravel. I have done a lot of searching, and none of the solutions seem to work for me.

Eloquent relationship for multiple table

I am working in laravel5.4. I have created four table as ticket, users, and company. Here, I have stored users id in ticket table. And stored company id in users table.
Here, I want to show ticket's user name with that users company name.
Here, I have create relation for it that looks like below.
Ticket model :
public function requesters(){
return $this->belongsTo('App\User','requester_id');
}
Here requester_id is who create ticket.
Users model :
public function company()
{
return $this->belongsTo('App\Models\Admin\Company');
}
Company model :
public function Users()
{
return $this->hasOne('App\Users');
}
Here, I have written query to get users information that looks like below.
Ticket::with('requesters')->orderBy('subject','asc')->paginate(10);
Now, I want to fetch company information or request_id So what changes should I have to do in this query to fetch company info along with ticket and users ?
If you want to to Eager load multiple relationship you can put them all in single with like this:
Ticket::with('requesters','requesters.company')->orderBy('subject','asc')->paginate(10);
but if you load nested relationship you can use shorter notation - you can omit parent relationship, so it's enough to use here:
Ticket::with('requesters.company')->orderBy('subject','asc')->paginate(10);
Try this, it should work for this case:
Ticket::with('requesters')
->with('requesters.company')
->orderBy('subject','asc')->paginate(10);

Laravel Datatables display data from related tables

I'm using Datatables for displaying data. It works good, but when I want to display data from related tables (models), they do not appear.
Model Keyword.php
public function website() {
return $this->belongsToMany('App\Website');
}
Model Website.php
public function keywords() {
return $this->hasMany('App\Keyword');
}
'websites' database table: id, siteName, siteUrl
'keywords' database table: id, website_id, kwName
Url that should display data: projects/'.$website->id.'/edit (example: projects/2/edit - (edit is a blade))
So, at this Url I want to show a datatable that display all kwName for certain website_id(in this example /2/).
Please, help me what should I use and how to do this.
Your Eloquent relationships are the wrong way around.
keywords has a reference to website via website_id, this is a belongsTo relationship.
website is referenced by many keywords, so this is a hasMany relationship.
Laravel 5.4 Eloquent Relationships

SEO friendly URLs with category/subcategories/article slug? [Laravel]

First of all, I have Article model and articles table in the database. Each article can be shown using Laravel's standard URI structure: www.example.com/articles/5 (where 5 the article id.). Each article has a slug field (slug column in the articles table) , so with Route Model Binding it is easy to change this and have a slug instead of id in the URI:
In RouteServiceProvider.php I just added:
public function boot(Router $router)
{
parent::boot($router);
\Route::bind('articles', function($slug) {
return \App\Article::where('slug', $slug)->firstOrFail();
});
}
... and now I can open articles with: www.example.com/articles/this-is-some-slug .
On the other hand, each article belongs to one category. For example, let's say that there are the following categories:
Politics
Sport
Football
Tennis
ATP
WTA
Culture
I created these categories by using Baum (an implementation of the Nested Set pattern for Laravel 5's Eloquent ORM). So there is a Category model and categories table in the database:
$table->increments('id');
$table->string('name');
$table->integer('parent_id')->nullable();
$table->integer('lft')->nullable();
$table->integer('rgt')->nullable();
$table->integer('depth')->nullable();
$table->timestamps();
Of course, in articles table there is a column category_id because of One-to-Many relationship (one Article belongs to one Category, one Category can have many Articles).
All articles belonging to some category can be displayed via the following URL: www.example.com/articles/category/1 (where 1 is the id). If we add slug column to the categories table & set Route Model Binding :
\Route::bind('category', function($slug) {
return \App\Category::where('slug', $slug)->firstOrFail();
});
then we will use a slug instead of id: www.example.com/articles/category/politics (this will display all the articles belonging to the category politics).
But I would like to have URIs with the following structure:
www.example.com/sport/tennis/wta/article_slug (/category/subcategory/subcategory/article_slug)
www.example.com/politics/article_slug (/category/article_slug )
and so on...
The problem is that I have no idea how to do this with Laravel. Is it even possible? How would you solve this problem?
Thanks in advance and sorry for my bad English.
SEO friendly URLs with category/subcategories/article slug?
To produce a url that's example.com/category/subcategory/article is pretty simple, but you're obviously looking to add the complexity of multiple subcategories. For that we'll need to look at route parameter regex constraints.
Using regex in your route parameter constraints you can get the subcategories as a string eg. subcategory1/subcategory2 and then pass it via another custom binding or directly to your controller.
Here's an example of the route you need with the route parameter constraint added:
// Route to closure
Route::get('/{category}/{subcategories}/{article}', function($category, $subcategories, $article)
{
return $subcategories;
})->where('subcategories', '(.*)');
// Route to controller method
Route::get('/{category}/{subcategories}/{article}', 'ArticlesController#show')->where('subcategories', '(.*)');
And an example of a custom binding for your subcategories parameter that returns the subcategories as an array:
$router->bind('subcategories', function ($value) {
return explode('/', $value);
});
Caveat: The only problem you will run into using the route parameter bindings in the way you've described, is that the article loaded here is only dependant on the slug being correct. It will still load if the categories in the url are unrelated, which you'll need to take care of in your controller logic.

Laravel > route, filter URL against database

I'm trying to build the new version of our site with the Laravel 5 framework.
The problem is, we have now the following site structure (and this may not be changed):
domain.com/[productname] <- dynamic
domain.com/[categoryname] <- dynamic
domain.com/some-static-page1
domain.com/some-static-page2
....
In our database, we have a list of different product names and different categories.
I want to route the [productname] to a productController and the [categoryname] to the categoryController. But therefor, i need query our database with each request to see if the URL parameter is either a product or a category.
Any ideas to get me on right direction?
Thanks in advance!
I think this would be the best way to do it:
Route:
Route::get('/cat={name}', 'CatagoryController#cataFind');
Route::get('/prod={name}', 'ProductController#prodfind');
Controller:
public function cataFind($name){
//get all the rows with that cata
$catas = Product::where('cata', $name)->get();
return view('cata')->with('catas', $catas);
}
public function prodFind($name){
//get all the rows with that prod name
$prod = Product::where('name', $name)->get();
return view('prod')->with('prod', $prod);
}
If you need any exsplnation on what's going on,, then just comment and I'll update the post. But this is the path you should be taking I should of thought!

Resources