Unable to access Laravel backend when creating new page? - php

So basically my site was pre-made so that pages did not exist. The only features that existed was a directory and some posts. No pages. So I had to duplicate the posts function (meaning I replaced all instances of $post and $posts with $page and $pages). It all worked fine. I created a test page which worked. But now I can't access my backend.
This is the URL structure of the pages as seen in the routes file:
Route::get('{url}', 'postsController#viewpage');
So it will basically look like this: www.mywebsite.com/pagehere
But the backend's URL structure looks like this: www.mywebsite.com/admin
So I wonder if my page structure is conflicting with the admin backend?
Whenever I try to access the backend, I get redirected to the homepage.
My controller file has this:
public function adminpage(){
return view ('admin/index')->with("title","Admin");
}

Seems like the rule you put in place matches every route:
Route::get('{url}', 'postsController#viewpage');
You need to specify that the given url must exclude routes tarting with admin, via a regex you can achieve this:
Route::get('{url}', 'postsController#viewpage')->where('url', '[^admin]');
Also I would advise you to use appropriate naming convations: PostsController
Laravel route regex docs

Related

Routing in Codeigniter 3

I'm relearning PHP and codeigniter but i can't help but notice how ugly my url looks.
So i have a pages controller: Say lets load homepage.
It would look like this:
public function home(){
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}
I guess thats decent as my url will probably look like this:
http://localhost/ProjectName/index.php/pages/home
But whenever i click on a link that involves more data processing for example lets say my home has a list of users and i click on one to view their profile. I ofcourse have to pass something to identify which profile i am to load so my link would look like this:
http://localhost/OnlineStories/index.php/pages/view_userprofile/1
In my controller id have:
public function view_userprofile(){
$userid = $this->uri->segment(3);
//process data then load view
}
I'm not asking for any advanced stuff but i would like to ask simple things to improve my url like correct standard for naming my controller functions.
For example: on previous instances of loading my homepage my function name was view_home now its simply just home so its cleaner.
Maybe i could rename my view_userprofile to simply user_profile.
Anyway is there any better method to passing values from view to controller other than uri segment? its what ive been using so i never gave it much thought. I've seen some use get() but idk if its better than uri segment?
Or should i stick with the uri segment and keep my url as controller/method/parameter?
I haven't really explored post-project polishing in codeigniter so idk if theres a way to polish/change the url(just the name).
Talk about URL in CodeIgniter, you never mention about application/config/routes.php file. Have you seen it ..?
In routes.php file you can create your URL name and mapping it to the controller/function that you have
like this:
$route['user/update'] = 'user_controller/update_data_function'
so, you can call URL http://your.site/index.php/user/update to execute your update_data_function() in your User_controller.php

Display dynamic URLs in CodeIgniter for seo

I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.

Routing in CodeIgniter for (:any)

I'm trying to make my CodeIgniter application work similarly to WordPress.
I want to be able to make these kind of URLs:
http://www.example.com/my-post-example
http://www.example.com/new-headline-here
http://www.example.com/i-love-stackoverflow
My routing:
$route['(:any)'] = "core/index/$1";
Which will call my Core controller and pass the page name into the index function.
I then lookup in my database for the page name and display the page to the user. So far so good.
However, there will be times when I want to call another controller. For example:
http://www.example.com/admin/edit_page/3
http://www.example.com/admin/settings
Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.
For example,
if ($page not in DB) {
// Call controller/method
}
This seems a little redundant since I just want CodeIgniter to handle this.
The routing rule you using it is OK for your purpose.
If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.
However you will get one problem if your link looks like this
http://www.example.com/my-post-example/test
It will try to go my-post-example controller and test method.
Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index
Finally If you call your other link with controller/method name it will be OK using your any rule

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Make title string the entire URI for all pages using CodeIgniter

With CodeIgniter I'm trying to create a URL structure that uses a title string as the entire URI; so for example: www.example.com/this-is-a-title-string
I'm pretty confident I need to use the url_title() function in the URL Helper along with the routes.php config folder but I'm stuck bringing it all together.
Where do I define the URI and how is it caught by the routes folder?
Seems to be a straight forward problem but I'm getting stuck creating the URLs end-to-end. What am I missing?
I thought about a catch-all in the routes folder: $route['(.*)'] = "welcome/controller/$1"; ....but how would this work with multiple functions inside a particular controller? ...and maybe it's not even the right way to solve.
You can send all requests to a driver with something like this:
$route['(:any)'] = "welcome/function";
Then use the _remap function to route requests inside the controller.
However, using URL's as you suggest limits the CI functionality. Try something better like www.example.com/article/this-is-a-title-string
$route['article/(:any)'] = "articles/index";
and in article (controller), use _remap...
If you're going to re-route every request, you should extend CI_Router.
The actual implementation depends on what you're doing. If you customize CI_Router, you can do it AFTER the code that checks routes.php, so that you can keep routes.php available for future customization.
If the URI contains the controller, function, and parameters, you can parse it within your extended CI_Router and then continue with the request like normal.
If the URI is arbitrary, then you'll need something (file, db, etc) that maps the URI to the correct controller/function/parameters. Using blog posts as an example, you can search for the URI (aka post-slug in WordPress) in the db and grab the corresponding record. Then forward the request to something like "articles/view/ID".

Categories