Laravel Routing Database - php

I want to save some page information in database with url term that can be shown in URL when that page is going to access via URL. For eg. consider I have a website name as www.example.com
now I have page in my site say www.example.com/about-us.
Now I want to save the details of about us page in database say title, description and url_name
so if I update the content of about us page and set url_name as "something-about-us".
When I am accessing link from my website for About Us. so it should redirect to page with URL www.example.com/something-about-us instead of previous url www.example.com/about-us.
How I can create this kind of database saved routes in Laravel?
Thanks for help in advanced.

here is my thought. I don't think Laravel provides such a solution to your problem, though not 100% percent.
What you can do is to rewrite your own route file. Firstly, save information about dynamic routes in database and when they are updated, make sure that related database entries are modified accordingly.
Secondly, when requesting for a route, search the database and return url as parameter to redirect.
I am afraid you have to do it yourself just like construct apis using Laravel. If you have any question, I am ready to help.

I can't test it on this moment... but maybe you can do something like:
Route::get('user/{pageid}', function($pageid)
{
$page_content = //Search DB for page content with url_name = pageid;
return View::make('page.content', array('page_content' => $page_content));
});

Related

Laravel 5 route returns view but changes url

Im not even sure if this is possible but what I am trying to do is this. I store different page modules in a database. When someone goes to a page with one of these modules, the pageController recognizes its a module and ( using the redirect path stored with this module in the DB ) redirects them to a get route which is how I know what info to pull and put on the page.
For instance, if someone was to go to /photo-album the pageController would recognize that was a page module and returns the redirect ( redirect/photo/albums ) and then redirects them to that route.
Route::get('redirect/photo/albums', ['uses' =>'PageController#getPhotoAlbums']);
The problem is the url then becomes /redirect/photo/albums. I would like it to maintain /photo-album
The reason I am doing it this way is because there will be several modules stored and each one will contain different things ( think blog, photo albums, video gallery etc. ). I need the redirect to figure out what goes on that page and what view to show. In this case PageController#getPhotoAlbums goes to the getPhotoAlbums method, pulls the photos and serves up the photoalbums view.
There may be a better way to do this and i'm open to it. Thanks in advance.
I don't have enough rep to comment, so I'll put this as an answer and hope it helps:
You really shouldn't get data from another controller. All your data should be available in the Eloquent model. This is important to the 'MVC' architecture.
There is more discussion on this here: Laravel: Load method in another controller without changing the url
So essentially, when your controller recognises it as a page module it should pull that data from the DB using Eloquent and then pass that to a View. That will mean that the original request URL is still retained.

Implementing a slug / clean URL system - general plan

I am currently working on a eCommerce style project that uses a search engine to browse 7,000+ entries that are stored in a database. Every one of these search results contain a link to a full description page. I have been looking into creating clean/slug URLs for this, my goal is if a user clicks on some search result entry the browser will navigate to a new page using the slug URL.
www.mydomain.com/category/brown-fox-statue-23432323
I have a system in place to convert a string / sentence into URL form. However, it is not clear to me what the proceeding steps are once these URL's are created. What is the general plan for implementing this system? Do the URL's need to be stored in a database? Am I suppose to be using post or get data from the search result page to create content in these full description urls?
I appreciate any suggestions!
Many thanks in advance!
Each product has a unique url associated with it in the database.
When you perform a search you just return the correct unique url.
That way you only ever work out what the url should be once, when the product is first added and that url will always relate to that one product. This is the stage you use your system to create that url
Maybe you can enlighten us as to if you are using a framework? Some frameworks (like Zend) have ini / xml files for routing. But you will still need to store the urls or at least the article slugs in a db.
Storing the entry urls in the db after they have been "searched" is necessary because you want slugs to stay the same for entries. This allows for better caching / SEO which will improve your sites usability.
Hope that helps!
Edit: Saw your question about pulling up individual articles. You will have to start by setting up a relation between your entries to urls in your database. Create a url table with url_id, and url. Then place url_id on the entry table. Then whenever someone goes to any URL search the url table for the current url, recall the url_id, and then pull the entry. At that point its just styling the page to make it look the way you want.
A common approach is to have a bijective (reversible) function that can convert a "regular" URL into a user-friendly URL:
E.g.:
www.mydomain.com/category/brown-fox-statue-23432323
<=>
www.mydomain.com/index.php?category=brown-fox-statue-23432323
Then you need not keep record of this mapping (convention vs. configuration).
Search StackOverflow for "User Friendly URL Rewriting" for information on how to achieve this automatically with Apache. This question is a good starting point.

How to achieve more semantic urls in code igniter?

I am just learning CI. I am trying to build my own CMS. I have successfully created a url structure www.mydomain.com/pages/getpage/2, where 2 is the id of the page.
However, I want to remove the '/pages/getpage/2' from the url. Instead, I want the domain to read www.mydomain.com/about, about being the title of the page, which is a column in the db.
I realise this is something to do with routing and maybe htaccess file.
I'm not sure what to put in routing to make the url as I want it.
If you want www.mydomain.com/pages/getpage/2 to route to www.mydomain.com/about, insert this line in to your routes.php
$route['about/(:num)'] = "pages/getpage/$1";
your URL structure will be www.mydomain.com/about/2, since you need that id as a reference to your content.
visit here for more details on routing

PHP - Replace page ID selected from MySQL database with alias name in URL

Not sure if title explains my problem well...
I have a site with a lot of pages (I call them "sections"). When users click on a page link, PHP script selects section content and infos from MySQL database, and show them to user. To avoid the display of section ID in browser addresses bar (the page is loaded through the knowledge of section ID in database), there is one different file associated to each section. In these files, PHP code passes ID to $_GET variable, and script loads section. The displayed URL is so http://site_domain/path/to/file_name.php.
Is there a way to avoid the creation of real file and to show, for example, an alias in URL? I thought to add a new column "alias" in database table sections to replace it in URL.
Hope my English and the explanation is clear...
I think you can create some sort of general controller which is a page, lets call it index.php and in that page you can pass some information about the section on your website through some GET parameter.
For example: http://andreswebsite.com/index.php?section=home
And then in your index.php code you can redirect the user to that specific section. You can have the section name in your database so no need to pass the ID.
Also you can look at the Apache mod_rewrite module which I think it could help you (I don't know if you use Apache as your web server)
Here is the documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I hope this has helped you.

How to load different content based upon URL used to visit site i CodeIgniter

I'm sure I'm not the first person who has thought about this but I haven't had any luck forming that proper search query in google to find the info. Here's what I'm wanting to do:
I have a CodeIgniter based site. I'm going to store basic content into tables in the db. I'm thinking that I would have the domain names stored in table to and use the unique id of the table row as the method of querying the appropriate content from the db for the rest of the views. For example. MyDomain.com is #1 in the row followed by YourDomain.com. If the visitor arrived at the site by typing YourDomain.com then somehow CI would "see" that and then query the content for that domain from the db.
Does this make sense? Has anyone else tried it? Is it possible?
Haven't done it myself, but I did some searching for "codeigniter multi site" and found some useful links, this being one of them that seemed to step you through the process.
In general there is an HTTP_HOST header (or similar header) that identifies the host in the user's request. You can look that header up and then use that to index into your database to extract the right content.

Categories