How Silverstripe URLs work? - php

I am new to Silverstripe Framework / CMS. I see ./mysite/code/Page.php as controller and ./themes/simple/... as template directory. I logged into the admin panel and added new test page. The menu appears on the website with the URL http://example.com/test and content is displayed.
So what I want to know is, how to access the new controller let say Download.ss. I want to access the URL http://example.com/download/123/ without adding the new page download in admin panel. Thank you.

First of all, any files with a .ss extension are template files not controllers.
Create a new class in mysite/code/Download.php which extends Controller.
class Download extends Controller {
public function index() {
// Automatically handles URLs like http://example.com/Download
}
public function exampleaction() {
// Automatically handles URLs like http://example.com/Download/exampleaction
}
}
After that you'll want to add a new routes.yml file to the mysite/_config directory to specify that the index function on your new controller should handle calls to http://example.com/download/123.
---
Name: downloadrules
---
Director:
rules:
'download/$ID': Download
Now the '123' portion of your example URL will be accessible as $this->request->param('ID') within the index function.
Now you can do:
class Download extends Controller {
public function index() {
$fileID = $this->request->param('ID');
// Do your thing.
}
}
Documentation for this stuff is at http://doc.silverstripe.org/framework/en/reference/director

Related

Laravel 7 Redirect Login Authorization

I'm currently using template for my view using jQuery, HTML, and CSS.
When I tried to use middleware to authenticate the user role before accessing routes ,I stumble upon this error.
Error:
View [authorization.login] not found.
Is it because I use a different name or custom login for my page? How can I change the link to my current login address?
Here are my routes
Route::get('/user-register-page', 'LoginUserController#registerUserPage')->middleware(['auth','auth.admin']);
Route::post('/register-user', 'Auth\RegisterController#create')->name('register');
And here's my controller
class LoginUserController extends Controller
{
public function registerUserPage(){
return view('user-register');
}
public function loginUserPage(){
return view('user-login');
}
// public function registerNewUser(Request $request){
// dd('test');
// }
// public function loginUser(){
// }
}
In your resources folder inside views you will find a folder called auth, in case it is not there, create a folder called auth and drag in your file user-login and rename it to login.

How to add a view?

I've been researching up on how to add views and I'm stumped. I want to add a view and all I get are 404 errors. All the examples I see on the web are just to add a default controller. I have a default controller, now I want to add a new page passed an ID in the URL.
This is the controller xyz.php:
class Xyz extends CI_Controller {
public function index() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_main_view');
}
public function activity() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_activity_view');
}
}
Model is xyz_model.php, and views are xyz_main_view.php and xyz_activity_view.php.
This is routes.php:
$route['default_controller'] = 'xyz'; // works okay
// $route['xyz'] = 'xyz/activity'; // 404
// $route['activity'] = 'xyz/activity'; // 404
// $route['xyz/activity'] = 'xyz/activity'; // 404
// ... many, many other different approaches
I'm able to use http://localhost, but I'd like to use the following:
// map to main view
http://localhost/index
http://localhost/xyz
http://localhost/xyz/index
// map to activity view
http://localhost/activity
http://localhost/xyz/activity
My understanding is that some of the URLs for the main view should work automatically, not seeing it. Just http://localhost.
I haven't even touched how to get an ID from the URL for the activity page. Just want to get over this first hurdle.
Keep this code in routes
$route['default_controller'] = 'xyz';
Then Try this URL to execute "activity()" function in xyz controller.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity
To Pass Parameters such as id's you can use this url.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity/[ID]
For more information please check codeigniter routing library. It is really easy to understand.
https://www.codeigniter.com/user_guide/general/routing.html

Where does CodeIgniter place newly created webpages?

I am building a web server locally using CodeIgniter and am trying to add a new page to my website. However, after creating the Controller and View files for the page I cannot access the new page at (what I think is) the desired address.
This is on a Pi running PHP 5 and Apache 2 with CodeIgniter 2.2.6. I have been able to access other pages to work through just writing the Controller and view files, but I can not access this one.
Controller page code (file name: Display_Live_State.php)
<?php
class Display_Live_State extends CI_Controller {
public function __construct()
{
//Construct page
parent::__construct();
$this->load->helper('url_helper');
}
public static function view()
{
$this->load->view('Display_Live_State_view', $data);
}
}
?>
View page code (file name: Display_Live_State_view.php)
<body>
<div>This is a test</div>
</body>
I expect to find a page with the test sentence on it at path localhost/index.php/Display_Live_State/view as I have created other pages with similar filepaths (except for the name of the controller) and have been able to access them.
If someone could enlighten me as to where CodeIgniter 'puts' the website when created, or show me what I did wrong / where the page is if it exists, I would be very grateful. Thanks.
Change Display_Live_State to Display_live_state both in the class declaration e.g. class Display_live_state and in the filename application/controllers/Display_live_state.php.
Set your base_url if you haven't already to http://localhost.
You should be able to access your controller via the url: localhost/index.php/Display_Live_State/view

"Action" in indexController not rendering in view

I have an existing Zend 1.12 project with modules et al that I am trying to simply add a view to create another web page. So there is a controller called "Management" that has an index action, the default of course, and the correct view page called index.phtml. That is all fine and works inside the entire project/website.
class Management_IndexController extends Default_Controller_Action {
function indexAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->setLayout('management');
$this->view->headTitle()->set('Management');
$message = new Com_Ui_Message();
$request = $this->_request;
$settings=new Com_Data();
$content=new Com_Data();
$data = new Com_Data($request->getPost());
$this->setFocus('#name');
}
function chatAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->view->testMessage = "test";
}
}
So if I browse to http://www.bmcmusicgroup.com/management I see the default action public function, and that is all good. If I try to add another public function called chatAction inside of the management controller, and create a view page called chat.phtml in the appropriate folder where the index.phtml file is (the management index.phtml file), and then point my browser to http://www.bmcmusicgroup.com/management/chat I get nothing.
This is an existing site that I am trying decipher. Any pointers would be greatly appreciated.
Try changing your url to the following and you should see the page: http://www.bmcmusicgroup.com/management/index/chat. The URL you entered (http://www.bmcmusicgroup.com/management/chat) decomposes like so:
Module: management;
Controller: chat;
Action: index

How can I redirect a 404 Error in a custom 404 page using Codeigniter?

Kind sirs,
I'm using Codeigniter to build a blog. I might need a way to redirect a 404 error into a custom 404 page. Just like what Abduzeedo.com's 404 page. Is it possible to control this by using routes? Or should i use controllers to direct it to another view? Thanks very much!
there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following
$config['subclass_prefix'] = 'MY_';
Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$base_url = $this->config['base_url'];
$_SESSION['error_message'] = 'Error message';
header("location: ".$base_url.'error.html');
exit;
}
}
Now Error controller will be your error page, where the page will be redirected for 404 error.
Custom 404 page is create by using the following 3 steps,
Step 1: First open routes.php in application/config and set a custom controller name,
$route['404_override'] = 'my404'; //my404 is class name.
Step 2: Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here,
<?php
class my404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Step 3: Create a view file with name 'error_404.php' with your custom message.
You don't need to create any controller or modify the code, rather than modify one view file. Just placed your 404 page HTML into the application/views/errors/html/error_404.php file.
If you want to use base_url() or any other CodeIgniter functions, initialize CI_Controller class like the below code.
<?php
$ci = new CI_Controller();
$ci =& get_instance();
$ci->load->helper('url');
?>
For a simple solution, go to your "error_404.php" file in "application/errors/" directory and put this code at the beginning of the file:
header("Location:".base_url());
It'll redirect your 404 page to home page.
Simple Solution.
Create your custom error or Page not found page within view.
Create a controller for error page. Within index function load the view of error page that you already created in previous step.
Now, Go to your application/config/routes.php
you should find $route['404_override'] = '';
Its the '' mean its calling the default error page.
Now place your controller name for error page .
It will work.
Suppose my Controller for error page is 'error'. and view page is 'notfound' .
So, code for Error.php controller will be:
<?php
class Error extends CI_Controller
{
function index()
{
$this->load->view('notfound');
}
}
Now, I replaced $route['404_override'] = ''; to $route['404_override'] = 'error'; in
application/config/routes.php.
Well you want to show your custom 404page when page not found in your website you just replace your custom file on core file which is located in application/error/error_404.php just replace your file here and you are good to go else where you can follow other method by using routes and custom controller for you custom 404 page. But the shortest and easiest way is replacing the core file with custom 404error page file
First open routes.php in the application/config folder and set a custom controller name.
$route['404_override'] = 'custom404'; //custom404 is class name.
Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.
<?php
class custom404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Create a view file with name 'error_404.php' with your custom message.
That is all you need to do and will be having your custom 404 page now.
No need to define any complexity.
The process is very simple.
First, go to your application->config->routes.php file and change this with your controller's function, where you simply load the header, footer, and 404.php page.
$route['404_override'] = 'welcome/_404';
Then come to your controller's page
function _404(){
$this->load->view("header");
$this->load->view("404");
$this->load->view("footer");
}
The same question is also available here
Display a view for 404 error in CodeIgniter
A more simple solution:
Just edit /application/views/error_404.php with your custom 404 page :)

Categories