I've installed on my server new Codeigniter installation and I would like to be able to pass to the default controller (welcome) GET parameters.
For example:
http://www.myserver.com/1234
and I would like the default index function on the welcome controller will get '1234' as GET parameter, but I cant make it to work any idea?
here is my controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
// echo get parameter here = 1234
}
}
And my .htaccess code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Your controller should be like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index($number)
{
//$this->load->view('welcome_message');
echo get parameter here = 1234
}
}
In your config.php you can enable query strings:
$config['enable_query_strings'] = TRUE;
The access to the method using this url:
http://www.myserver.com/?id=1234
I think that should work.
On your route.php file
// Would leave as your default controller
$route['default_controller'] = 'welcome';
// But add get query route here
$route['number='] = 'welcome/index';
You need to have ? at the start of the query get and then after that any other url query use &.
And then on controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
echo anchor('?number=1234', 'Home', array('target' => '_blank'));
// http://www.example.com/index.php/?number=1234
echo '</br>';
echo $this->input->get('number');
$this->load->view('welcome_message');
}
}
When refresh page you should be able to see the anchor link which you can click on then will open new page and should display the numbers.
You also may come up with error disallowed uri
Then go to config and use ?&=
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?&=';
You could alt use uri segments
<?php echo $this->uri->segment(1);?>
Use remap function
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
return call_user_func_array(array($this, $method), $params);
else
return call_user_func_array(array($this, 'index'), $params);
}
public function index($number)
{
//$this->load->view('welcome_message');
echo get parameter here = 1234
}
}
Related
I'm new to Laravel and have just added the Authentication package to an existing project.
Upon logging in, I want to be redirected to /Result a page that that I know works using a controller. If I type the URL /Result the page loads correctly but when I login I am being redirected to index each time rather than /Result
Routes
Route::get('/result','ResultsController#getResults')->name('result');
Auth::routes();
Route::get('/', 'HomeController#index')->name('/');
Home Controller
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('result');
}
}
Results Controller
class ResultsController extends Controller
{
public function getResults( )
{
$results = Result::all();
return view('/result', ['results' => $results]);
}
}
Login Controller
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = 'result';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* #return
*/
public function authenticated()
{
return redirect()->route('result');
}
}
So far I can load index and be redirected to login, when I login I want to be redirected to /Result but instead I recieve an Undefined variable: results.
I have jumped to /Results by manipulating the URL and the page /Results does work.
Any help would be much appreciated, just le me know if you need any additional code examples from any other files.
thanks
James
First of all change the route name format use only result.
Route::get('/result','ResultsController#getResults')->name('result')
For redirect any route you can use LoginController authenticate method.
\App\Http\Controllers\Auth\LoginController.php
Add this method to that controller:
/**
* #return
*/
public function authenticated()
{
return redirect()->route('result');
}
I want specific htaccess file for specific route.
For example, i want to allow all ips to home route of my site but prevent access specific ips to just payment route or specific routes.
Please help me with this problem.
you can use three solution:
laravel middleware
code Source
here:
namespace App\Http\Middleware;
use Closure;
use Symfony\Component\HttpFoundation\IpUtils;
class RedirectInvalidIPs
{
protected $ips = [
'65.202.143.122',
'148.185.163.203'
];
protected $ipRanges = [
'10.11.3.1',
];
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
foreach ($request->getClientIps() as $ip) {
if (! $this->isValidIp($ip) && ! $this->isValidIpRange($ip)) {
return redirect('/');
}
}
return $next($request);
}
protected function isValidIp($ip)
{
return in_array($ip, $this->ips);
}
protected function isValidIpRange($ip)
{
return IpUtils::checkIp($ip, $this->ipRanges);
}
}
.htaccess
code Source
here:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents
RewriteCond %{REMOTE_ADDR} !=111.111.111.111
RewriteRule ^.*$ /maintenance.php [R=302,L]
Nginx allow and deny IP
CloudFlare
How do I control IP access to my site?
You should use middleware for access control, the .htaccess 'can' technically be used for this, but it is highly recommended not to.
Rough example of possible logic to filter based on ip, that can be put in the handle function of newly created middleware:
if (!in_array($request->ip, ['127.0.0.1', '::1'])) {
abort(403);
}
return $next($request);
I placed the 'dompdf' # '/system/libraries/', and create a class file 'Dompdf.php' at the same dir, code as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
class CI_Dompdf extends Dompdf {
/**
* Set the template from the table config file if it exists
*
* #param array $config (default: array())
* #return void
*/
public function __construct($config = array())
{
log_message('info', 'Dompdf Class Initialized');
}
}
below is part of the controller function which using the dompdf library:
public function pdf_create($html, $filename='', $stream=TRUE){
$this->load->library('Dompdf','dompdf');
$this->dompdf->load_html($html);
$this->dompdf->render();
if ($stream) {
$this->dompdf->stream($filename.".pdf");
} else {
return $this->dompdf->output();
}
}
but I get the error below:
PHP Fatal error: Call to a member function isHtml5ParserEnabled() on null in /home/mysite/public_html/ci/system/libraries/dompdf/src/Dompdf.php on line 492
I'd checked the function does exists in 'dompdf/src/Options.php'.
I have no idea how to solve it.
My dev env:
Codeigniter 3.0.6
Dompdf 0.7.0
Thanks.
UPDATE Found error occurred due to not call Dompdf constructor, correct code as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
class CI_Dompdf extends Dompdf {
/**
*
* #param array $config (default: array())
* #return void
*/
public function __construct($config = array()) {
parent::__construct($config);
log_message('info', 'Dompdf Class Initialized');
}
}
I'm using below code for my site, but it displayed me
404: Page not found.
routes
$route['class_name/function_name/(:num)'] = 'class_name/function_name/$1';
Controller.
public function function_name($Id)
{
print_r($Id); exit;
}
Use URI to extract the id from URL:
Following is the link in docs : http://www.codeigniter.com/userguide2/libraries/uri.html
Example- How to get the a URL using uri_string() codeigniter?
I have create a simple one maybe it could help you out
this is my controller welcome.php fresh from codeigniter
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
public function test1($ID) {
echo $ID;
}
public function test2($ID, $ID2) {
var_dump($ID, $ID2);
}
public function test() {
echo 'test';
}
}
and here is my route
$route['default_controller'] = 'welcome';
$route['welcome/test1/(:num)'] = 'welcome/test1/$1';
$route['welcome/test/(:num)/(:num)'] = 'welcome/test/$1/$2';
example to access it if working
https://192.168.248.209/stackoverflow/welcome/test2/1/3
https://192.168.248.209/stackoverflow/welcome/test1/1
this one is dynamic according to your server implementation
See Pretty Url Setup CodeIgniter
I am starting out with codeigniter, but the documentation is horribly written and doesn't actually work with the new version. My issue is that I cannot call a function within a model.
Here is my model: User.php
<?php
class User extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function test($x)
{
return $x;
}
?>
And my controller: welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->model('User');
echo $this->User->test('darn');
$this->load->view('welcome_message');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>
Check your {}s, your test function is outside your User class. Move it inside the class, then $this->User->test() will work.
<?php
class User extends CI_Model {
function __construct()
{
parent::__construct();
}
function test($x)
{
return $x;
}
}
?>
There's nothing "horrible" about the documentation.