I'm creating a Laravel 5.1 application that lists products on pages. Now each product on the database has the url of the product detail page. I have done a method that exports all those urls and convert's them to laravel routes, and the routes are written into a file and included on the laravel routing. I have done that on that way in order to be able to optimize the routing using laravel's routes:cache command. Now my question in fact is about optimization, which way would be better, to have a file with all routes, let's say 100K routes or to have a single entrance point that compares the route in question inside the database and return the respective product.
There isn't a need to have an individual route for each product. Possibly Store a unique slug(Semantic URL) in the database? Then have one route that displays a product based on a what is passed to the route.
user friendly slug could be something like 't-shirt-9000'. If you don't want this, you can always use the unique id you already have set for the product in the database.
Within your show method, you would need to query the DB with the slug past in the request to the database.
quick example:
In routes.php
Route::get('/products/{product}', 'ProductController#show');
Product Controller
public function show($product)
{
$productRequested = Product::where('url_slug', $product)->first();
// Do whatever from here
}
the fact that you use a framework you will be enforced to optimize your code
route file allow you to get the fastest way to minimize the number of mapped URLs.
the notion of route offered by Symfony to laravel allow you to manipulate all variables of your url with one single route , for example :
route::get('{category}/{id}/{slug}/{var1}/{var2}/{var3}/{varX}','yourController#yourMethod');
this route will send all variables in the url to yourMethod();
Related
I am a basic user of code igniter and PHP.I have some products page with filters. How to created a URL link like this:
http://somepage.pl/products?color=red
I know I can do above url when I will change the config line:
$config['enable_query_strings'] = FALSE; on TRUE.
But I want to use this option only one controller and one function.
Your best bet is to rely on codeigniter's native URI rewriting.
By default, (I'm not getting into custom routes here, but you might if you understand them) URLs served by Codeigniter will look like this:
BASE URL/Controller/method/params
Base url will be what you define in the config and more often than not, it'll be the base domain of your site, like example.com.
Since CI is built based on MVC architecture, all your functionality must "live" on different methods within one or many controllers. So, for example, you might have a controller named products and within that controller you may have a method (for simplicity's sake: function) called lookupProductById that will take one parameter ($product_id). It'll look like this:
class Products extends CI_Controller {
public function lookupProductById($product_id = null)
{
// whatever you need to do (like querying the database to fetch info for the product with a certain product ID) goes here
// for instance, start by checking that the product ID was passed in the URI
if ($product_id == null)
{
// handle exception
}
else
{
// query the database and fetch info for the product whose ID is $product_id
}
}
}
so, when accessing example.com/products/lookupProductById/8 you'll be able to fetch the info related to product ID 8
You may want to read the CI documentation (the introductory chapters and tutorials will guide you through a (very) basic understanding of how MVC frameworks operate, how controllers, models and views interact to produce a result, etc.) to better understand what you're getting into :)
I have a problem and hopefully we will solve it.
The problem is, I am creating an application this application has an “Admin Panel (Backend)” and Frontend.
The URL structure of the admin panel is like this
https://www.app-name.com/admin
For frontend I have a different URL structure which is based on country for example.
https://usa.app-name.com
https://ca.app-name.com
https://uk.app-name.com
https://uae.app-name.com
https://in.app-name.com
It means in frontend I have multiple sub-domains. And the all these sub-domains are sharing a single database. As well as, backend is also sharing the same database.
Now my problem is how can I handle front-end like this.
Possible one solution in my mind is to copy app to all the sub-domains and connect with single database. And for admin panel copy it at the main domain.
But in this case, if I have a single modification to application I have to update all the copies. I want to use only single copy of app to handle all sub-domains.
Anyone have some solution for this problem. Thanks
I'd suggest executing some code in a important model for the app that gets auto-loaded through the entire app. Maybe call it Site_model.php.
Firstly, lets autoload the model. Open up config/autoload.php and add the model into the list of models to be autoloaded:
$autoload['model'] = array('Site_model');
Now, create the new model calls Site_model. In the construct of the model, add some code along these lines (this is a simply basic example of how to fetch and assign the value of a subdomain. I imagine there are better ways, but for this example, it will do):
class Site_model extends CI_Model {
var $subDomain = '';
function __construct() {
$this->subDomain = array_shift((explode('.', $_SERVER['HTTP_HOST'])));
// further code which uses the value of subDomain to fetch the correct record from the database.
}
Now, anytime you want to reference the subdomain in your code, you can simply get it like this:
echo $this->Site_model->subDomain;
This should also work for the admin backend. Oh, and ensure that the model is the first to be auto-loaded, in case any of the other models that are auto-loaded initialise code that is dependent on the site model's values.
How can I create duplicate routes?
I want to have SEO and user friendly routing like:
/{product.slug}
/{category.slug}
But how does Symfony know which one to render? It now defaults to the last definition.
You can create you route with two variables:
/{type}/{slug}
type would be productor category
Or, if you want only one variable, you can adapt your function depending on the value passed by URL.
I think I understand the concept of HMVC after reading this question and answer https://softwareengineering.stackexchange.com/questions/220480/hmvc-and-database-connections; an extract from an answer is below:
Let's assume you want to have a view that enables a user to make a
comment to a blog post. You would have fields for name, e-mail, title
and comment, but you also want to have a field country displayed as a
dropdown. In the action that displays this view you would make a
database query that loads the countries and then populate that
dropdown. Which is ok, but it forces you to duplicate the query and
the view required to display the countries if you need it in another
part of your application. A better approach would be to create
separate controller for countries with an action that returns a view
with the dropdown and then render that action whenever you need to
show a list of countries.
What I cannot wrap my head around is that if I can internally request a controller/model/view which just displays a widget (e.g. a country select box), doesn't that mean that by accessing that url from a browser will also just show that view?
How is this managed in HMVC, are routes defined as internal/external only, so matching an internal route with an external request would show a 404 page?
Is this generally how it is done and is the HMVC description/definition above satisfiable with the general use case of it in most web applications?
Showing the output of a sub-request in the browser shouldn't be a problem, so I wouldn't bother, especially that those URLs are not known by the user and it's safe to output the widgets separately.
Despite the above, you could, as #deceze mentionned, not attach those controllers to any routes. If you have a "default" route (matching all requests), then you would have to disable it.
I'm building a rbac in CI 2.1.4. I don't want to use opensource rbac system, because I'm building on an existent system. My resource is designed to: module_name::controler_name::action_name, I want to get the module_name::controler_name::action_name for the current executed action, eg. controllers/zoo/suggest.php's index action. so the module name is zoo, controller name is suggest, action name is index.
But when I'm in this index action, how I can get the zoo,suggest? Yes, it's simple to write some code to get them, but I have hundreds of actions and controllers. I found this way: http://stackoverflow.com/questions/11372277/how-can-i-create-a-method-that-gets-called-every-time-a-public-method-gets-calle, but I don't want to write nearly same code in every controller. I just want to write once.
I want to use post_controller_constructor, but this hook does not support the acquiring of my would-be-executed method and controller and module name.
How can I succeed my goal?