• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding admin area

#1
Hi,

I created the frontend of my app and now I'd like to create an admin dashboard.

I created an Admin.php controller with the following content:


PHP Code:
<?php

class Admin extends CI_Controller
{
 
     public function view($page 'dashboard')
 
     {
 
           if(!file_exists(APPPATH 'views/admin/' $page '.php')) {
 
                 show_404();
 
           }

 
           
            $data
['title'] = ucfirst($page);

 
           $this->load->view('templates/back/admin_header');
 
           $this->load->view('admin/' $page$data);
 
           $this->load->view('templates/back/admin_footer');
 
     }



I created  dashboard.php in views/admin

but I cannot access the page. If i try to access it like this: http://localhost:8080/ci/admin/view/dashboard
then it's ok, but I'd like to access the admin dashboard like this: http://localhost:8080/ci/admin/

I guess my route is incorrect, I used this:


PHP Code:
$route['admin'] = 'admin/dashboard'

i'm new to CI and I'm just tinkering with it to learn, but I can't figure this out. Any help is appreciated.
Thanks
Reply

#2
@TamasD

Here is a link to the documentation about URI routing ( https://codeigniter.com/user_guide/gener...ht=routing )
Reply

#3
(07-12-2018, 04:15 PM)php_rocs Wrote: @TamasD

Here is a link to the documentation about URI routing ( https://codeigniter.com/user_guide/gener...ht=routing )

Yes, i checked it already, but not sure what would be the solution for me.
Reply

#4
Hi,

Your route should be:



PHP Code:
$route['admin'] = 'admin/view'

This will work as your method default to 'dashboard'
A good decision is based on knowledge and not on numbers. - Plato

Reply

#5
The short version is your route is looking for the Admin Controller with a 'dashboard' Method.  It should be looking for the Admin Controller with the View Method.  You're close, almost there.


To expand and explain a bit on previous answers;  

I've assumed you've not removed index.php from your URL below and in application/config/config.php `base_url` = 'http://example.com'

according to your controller's default $page = 'dashboard'
PHP Code:
$route['admin'] = 'admin/view'
would give you the dashboard.php view when you call the URL http://example.com/index.php/admin/


To change the page, you would call something like this:

http://example.com/index.php/admin/settings
http://example.com/index.php/admin/themes

etc...

which will load the views/settings.php and views/themes.php files respectively.


Lastly, you don't *need* to define a route (unless you want to) usually, that controller would be accessible without any routes defined via:

http://example.com/index.php/admin/view
http://example.com/index.php/admin/view/dashboard
http://example.com/index.php/admin/view/settings
http://example.com/index.php/admin/view/themes

since magic routing is available all the time.  

The "magic" routing is: http://example.com/[controller]/[method]/[$param1]/[$param2]... etc

Good luck and welcome to CodeIgniter!
Reply

#6
@enlivenapp and @salain: thanks very much Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.