I'm building an admin section and I want to keep urls looking nice but more importantly my code well structured.
I currently have a URL like this
/admin/
which works fine. Another URL like this
/admin/users
which also works fine
However what I now want is an admin page to add a new user which would have the url
/admin/users/add
I cannot seem to get anything to change with that URL, it always triggers the admin/users controllers.
Would appreciate any help
Have you set up a route for this? Controllers by their nature allow you
{Controller Name} / {Controller Action}
To set this up the way you want them you will need to use a route
Route::get('admin/users/add', 'admin#user_add');
This will route all requests to 'admin/users/add' to the admin controller using the method 'get_user_add' or 'action_user_add' depending on whether the controller is restful.
Related
So I am working on a project and I have decided to use php laravel framework, but when it comes to things like creating a new page and dealing with page redirects etc, am I right in thinking that all these will be handled in the route/web.php file so all the pages for my application will be defined in the route along with the view ?
I was thinking what if my application grows to have dozens of pages is it best practice to define each one on the route or are there better ways to handle this?
Yes, the best way to do routing is laravel to have each route for . each page, the only exception is when you have dynamic routes, for example, if you have a route that checks for users id or category for some product etc and it looks something like this Route::get("/product/{$category_id}","FrontController#methodForGetingProduct").
And later in Controller, you define what will you send and receive of the information and what view should be returned.
you can follow this type routing .......
Basically I'm trying to make the admin section of a website harder to find. I have a Controller named built like this
class Admin extends MY_Controller{ // actions below
I like having the name Admin for the controller since it tells any future developer that might work on the site that this controller contains the admin section of the website.
However; as an extra layer of security, I don't want the admin section reachable by simply typing into a browser www.theDomain.com/admin. I would much rather that any route starting with "admin" return my standard 404 response.
I was hoping this could be achieved in the routes file with something like
$route['admin'] = 'error/404';
I would even accept being able to do this in the constructor of my controller. Thoughts anyone?
You can use different Class name for Admin Controller like "Admin48957". In this case, only who knows this Class Name can access the admin section by typing www.theDomain.com/admin48957. (This is the strategy of many modern CMSs. i.e. prestashop)
If you force return it 404. Then you also could not access by yourself.
But if you still want to return 404. You can use show_404() CodeIgniter function.
Perhaps you could call show_404() in your controller method.
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.
I have set up a simple app for now using ReactJS for the frontend and Laravel(5.1) for the backend.
All is OK but I woudl like to make the URLS look normal, as in the removal of the '/#' from the URL.
So
example.co.uk/#/about
becomes
example.co.uk/about
This is not an issue as I achieve this, but when I do it activate it, then the Laravel routing kicks in and flags a route error.
Is it possible to prevent Laravel form activating / doing this so that ReactJS takes over and works.
If so it would also be nice, so that if a Laravel route is set / wanted then it does use that one.
EG: These are ReactJS routes that would have used the '/#'
example.co.uk/about
example.co.uk/details
example.co.uk/listings
Then if I got to the following URL's then they are controlled by Laravel's routing?
example.co.uk/api/...
example.co.uk/admin/
Thanks
You need a catch-all route that will execute the same main controller action regardless the URL. Add the following to your routes.php
Route::get('{path?}', 'Controller#action')->where('path', '.*');
This way all URLs will go to Controller#action, that should display the base view for your application - the one that runs the ReactJS application.
I’m a recent user of Codeigniter and am developing a simple backend CMS to manage pages.
Based on a URL (in this example I have hidden “index.php”) : mysite.com/pagename
I would like the system to detect if there is a value of “pagename” in my database, if there is, I need the system to re-route to a custom controller (eg: Pagemaker) and if there is no record called pagename, just do it’s normal thing (i.e. find a controller called pagename)
Currently I have:
$route['(:any)'] = "pagemaker/create/$1";
whereby all requests are forwarded to my custom function.
However I want to change this structure so that if the page does NOT exist in the db, the traditional codeigniter request process is followed.
Can anyone offer any advice about how to complete this? Or any advice about routing custom CMS’s in codeigniter in general?
The best solution is to upgrade to CI 2.0 because it's stable enough and it gives you plenty of useful features.
In your case, set the following route:
$route['404_override'] = 'pagemaker';
If the router doesn't know where to go it just goes to pagemaker controller. This can then check if the first uri segment exists and if not you create a custom 404 page instead of the crappy default one.
And I don't want to hear any of this "Oh but it's not released yet" crap, I've been using it CI 2.0 for almost a year. ;-)
I can think of two possibilities:
1) Edit your custom function to let it redirect your client when page's not in the db
pseudo code:
if($dbresult == null){
redirect("http://yoursite.com/"+$this->uri->segment(3));
}
2) Edit the router class of CI so it will first check if the page's in the db and if not, just continues. This may be somewhat messier as you need a db connection in your Router.php