I'm just trying out Fat-Free Framework and I come to some trouble.
I try to use the autoloading to load one of my routes, like this :
<?php
$f3 = require 'vendor/bcosca/fatfree-core/base.php';
$f3->set('DEBUG', 3);
$f3->set('AUTOLOAD', 'app/');
$f3->config('app/routes.ini');
$f3->run();
I have a app/ dir, and a routes.ini file in it, like this :
[routes]
GET / = Test->show
Then, I have a Test.php file in app/, with this in it :
<?php
class Test {
function show($f3) {
echo 'ok !';
}
}
After running this I get a big fancy error saying the following :
Method Not Allowed
HTTP 405 (GET /)
Any ideas ? (It isn't my PHP Version, if you'd ask yourself)
Your initial code is fine. It's just that your Test class conflicts with the framework's own Test class.
Rename it to anything else and it should work.
Well, I tried some things. This actually works :
The index.php file is the same as before.
The app/ dir now has a controllers/ dir in it.
There is a file named index.php inside the controllers dir, with this in it :
<?php
namespace Controllers;
class Index {
public function get() {
echo 'Yey !';
}
}
The routes file now looks like this :
[routes]
GET / = Controllers\Index->get
And that's it ! It works.
Related
I'm new in php coding and I'm using Yii2 framework
I'm trying to make a simple project as follow:
I added a .php file named "PostController.php" in the backend/controllers and I wrote this codes in this file:
<?php
namespace backend\controllers;
use yii\web\Controller;
class PostController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
}
?>
and I created a file in beckend/views named "post" and in this folder I created a.php file named index.php
then I just wrote one line in index.php for test as bellow:
<h1>Hello World</h1>
Now I want to see this index file (Hello World) in my browser. Which url I should enter in my browser to see that? I tried the bellow url and it didn't worked!:
projectname.loc/index.php?r=post/index
if pretty url is true then you have to access the controller like
localhost/projectname/backend/web/index.php/controller/action
if pretty url is false then you have to access the controller like
localhost/projectname/backend/web/index.php?r=controller/action
if you project is in your local machine, if its on your server then you have to write server name instead of localhost
You should go to projectname.loc/post (or projectname.loc/post/index), to be in this action. before that you have to turn on the "prettyUrl" in your urlManager
If you use basic tamplate from yii2 your namespace must be app/backend/controllers and you should go to projectname.loc/index.php?r=backend/post/index
"backend" it will be a path to your controller, not a module
I deployed a Laravel 5 project to a shared hosting account, placing the app-files outside of the www-folder, placing only the public folder inside of the www-folder. All like explained here, in the best answer: Laravel 4 and the way to deploy app using FTP... without 3. because this file doesn't exist in Laravel 5.
Now when I call public_path() inside of a controller i get something like my-appfiles-outside-of-www/public instead of www/public. Does anyone know why I doesn't get the right path here? Can I change this somewhere?
Thanks!
You can override public_path using container.
Example:
App::bind('path.public', function() {
return base_path().'/public_html';
});
$app->bind('path.public', function() {
return __DIR__;
});
write above code in public_html\index.php above of this code
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
There are a number of solutions, like a change to index.php or the accepted one with the ioc container.
The solution that works best, in my opinion, is described in this SO question:
Laravel 5 change public_path()
Why is it better than other solutions? Because it works for regular http request, use of artisan ánd use of phpunit for testing! E.g. the change to index.php works only for http requests but leads to failures when using PHPunit (in my case: file (...) not defined in asset manifest).
A short recap of that solution:
Step 1: In the file: bootstrap/app.php change the very first declaration of $app variable from:
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
to:
$app = new App\Application(
realpath(__DIR__.'/../')
);
This points to your own custom Application class, which we will create in step 2.
Step 2: Create the file Application.php in the app folder:
<?php namespace App;
class Application extends \Illuminate\Foundation\Application
{
public function publicPath()
{
return $this->basePath . '/../public_html';
}
}
Be sure to change the path to your needs. The example assumes a directory structure like so:
°°laravel_app
°°°°app
°°°°bootstrap
°°°°config
°°°°...
°°public_html
In case of the OP, the path should probably be
return $this->basePath . '/../www/public';
(up one from laravel_app, then down into www/public)
I am working in Codeigniter with smarty templates.
Problem is that if i go about 2nd subfolder in view or controller, the Codeigniter stops working...
e-g here
application/controllers/main.php - works
application/controllers/admin/dashboard.php - works
application/controllers/admin/manageUsers/ListUsers.php - not working
I have searched the web, and they said that i can work with routes, which might do work with controller..
but it is views that i am concern of..
i mean admin views should go under admin folder, but i can not create subfolder in admin, if i put all views in admin folder, it will be a mess, nothing organized. i hope you are understanding what i am trying to say.
e-g
themes/default/views/home.tpl - works
themes/default/views/admin/dashboard.tpl works
themes/default/views/admin/site_settings/preferences.tpl not working
please can anyone guide me how to fix these issues.
Your problem is quite common. I've had it when I started working with CodeIgniter as well. What I found out to be the best way to overcome it, was to create a Custom_Router, which extends the CI_Router class. Doing that allows you to overwrite the controller class include/require logic and allow the usage of subdirs. This is a working example:
<?php
class Custom_Router extends CI_Router
{
public function __construct()
{
parent::__construct();
}
public function _validate_request($segments)
{
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
{
return $segments;
}
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.DIRECTORY_SEPARATOR.$segments[0]))
{
// Set the directory and remove it from the segment array
$this->directory = $this->directory . $segments[0] .DIRECTORY_SEPARATOR;
$segments = array_slice($segments, 1);
}
if (count($segments) > 0)
{
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
{
show_404($this->fetch_directory().$segments[0]);
}
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
{
$this->directory = '';
return array();
}
}
return $segments;
}
show_404($segments[0]);
}
}
?>
The code above works fine with as many subdirs as you'd want, although I wouldn't advice using this approach. I usually specifically state the path to my controllers in the route.php file.
Regarding your second problem - the templates. I never liked messy code and even messier viewers which contain <?php echo $something; for(...){}; foreach(){}... ?> all over the place. For me, that makes the code really hard to read and specially harder to debug. That's why I've been using the Twig template engine. There are tutorials how to get it working in CodeIgniter (I've used this one). Once you're done with it, in your controller you would simply need to write:
class Login extends Custom_Controller
{
/**
* Index Page for this controller.
*/
public function index() {
$data = array();
$error_message = "Invalid user!";
if($this->session->getUserId() != null){
redirect('/welcome');
}
// other logic....
// depending on how you configure twig, this will search for "login.html.twig"
// in "application/views/". If your file is located somewhere in the subdirs,
// you just write the path:
// admin/login.html.twig => application/views/admin/login.html.twig
$this->twig->display('login.html.twig', $data);
}
}
If Twig is not an option for you, then you will need to create a new class which extends the CI_Loader class and overwrite the public function view(){} method.
By the way, if you're creating a web application with a backend, you might find it easier to manage and maintain your code if you separate your applications in different directories. If you choose to go this way, you will need to create application/public and application/admin folders preserving the directory structure of a CodeIgniter "application". Here are the steps:
Create separate applications
/applications
-- public (a standard application directory structure)
---- cache
---- config
---- controllers
---- models
---- views
---- ...
-- admin (a standard application directory structure)
---- cache
---- config
---- controllers
---- models
---- views
---- ...
Open /index.php and change $application_folder to point to applications/public
Create a copy of /index.php, name it backend.php (or whatever you want). Open the file and change $application_folder to point to the applications/admin folder.
Open .htaccess and add a rule to pass all URI starting with /admin to backend.php
# Route admin urls
RewriteCond %{REQUEST_URI} admin([/])?(.*)
RewriteRule .* backend.php?$1 [QSA,L]
Put THIS file into application/core/
Filename must be MY_Router.php
What I want to do is creating a file like my_custom_settings.php in config directory and call the defined variable in view part.
let's say in my_custom_settings.php:
define('TEMPLATE_DIR', 'assets/front');
and in view part direct in HTML:
<link href="<?=TEMPLATE_DIR?>/stylesheet/style.css">
or any other alternative solution??
PS: Now I am using base_url() to access the path
personally i extend the /core/helpers/url_helper.php , defaults are site_url() , base_url(), current_url(); etc ... i just extended that for having base_static_url();
so put in core/helpers/url_helper.php:
if ( ! function_exists('base_static_url'))
{
function base_static_url()
{
$CI =& get_instance();
return $CI->config->slash_item('base_static_url');
}
}
then in config.php file you just add 1 more line:
$config['base_url'] = "http://mysite.com/";
$config['base_static_url'] = "http://mysite.com/static/"; //path to your static resources folder
then you can call static resources using :
<img src="<?php echo base_static_url();?>img/myimage.png"/>
ok this is might be more then what you are looking for,
but this is a way to put site wide configs in one file, and then easily have them available
in config folder you have file: my_custom_settings.php
in that file you want to set a config value like:
$config['TEMPLATE_DIR'] = 'assets/front' ;
$config['site_slogan'] = 'Laravel? Never heard of it' ;
create another file called: My_custom_settings.php
put that file in: application/library/My_custom_settings.php
that file will contain:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_custom_settings
{
function __construct($config = array() )
{
foreach ($config as $key => $value) {
$data[$key] = $value;
}
// makes it possible for CI to use the load method
$CI =& get_instance();
// load the config variables
$CI->load->vars($data);
}
} // end my custom settings
now in your controller constructor
public function __construct() {
parent::__construct();
// Load configs for controller and view
$this->load->library( 'my_custom_settings' );
$this->config->load( 'my_custom_settings' );
} // end construct
Now for the cool part -- anything you put in that config file, will be available for your controller and views. ( you can load config in a model constructor as well ).
in a controller or model you get the value with $this->config, like
$this->config->item( 'site_slogan' )
a little awkward, but for views, heres the reward, you only need the config name
echo $TEMPLATE_DIR . '/somefile' ;
Images, css, javascript, pdfs, xml... anything that is allowed to be accessed directly should not be living in your application directory. You can do it, but you really shouldn't. Create a new folder at the root of your directory for these files, they should not be mixed into your application, for example: in your views folder.
Chances are, you're using an .htaccess file, which is only allowing certain directories to be accessed via http. This is very good for security reasons, you want to stop any attempt to access your controllers and models directly. This is also why we check if BASEPATH is defined at the top of most files, and exit('No direct script access.') if not.
To get the correct path to these resources (js/css/images), you can't use relative paths, because we aren't using a normal directory structure. The url /users/login is not loading files from the directory /users/login, it probably doesn't even exist. These are just uri segments that the bootstrap uses to know which class, method, and params to use.
To get the correct path, either use a forward slash (assuming your app and assets are in the root directory, not a subdirectory) like this:
Or your best bet, use an absolute url:
// References your $config['base_url']
" />
Equivalent to:
http://mydomain.com/images/myimage.jpg
There are helpers built into CI that you can optionally use as well, but this is really all you need to know.
I have created a zend project on ubuntu which is in /var/www/student/ directory.
Now I have head.phtml file in this location:
/student/application/views/scripts/index/head.phtml
When I try to include head.phtml file in
/student/application/modules/test/views/scripts/all/index.phtml
Like this:
echo $this->partial('index/head.phtml');
It gives me following error:
Message: script 'index/head.phtml' not found in path (/var/www/student/application/modules/notification/views/scripts/)
Including files is always a difficult job for me. How to fix this. I have to include this file in many modules then what is permanent solution for this that I should not guess the path
Thanks
You can add several path to look for script view files. The best way is to do it in the bootstrap file for all your common files (like head, footer, metas...).
Just add a setupView method in your bootstrap where you deal with everything which is realted to your views :
protected function _initView()
{
$view = new Zend_View();
// setup your view with jquery and other stuffs...
// [ ... ]
// add the view directory to the stack of scripts paths
$view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
}
<? $this->setScriptPath('/student/application/views/scriptss'); ?>
<?= $this->partial('index/head.phtml') ?>
I added following line in Controller's init() function.
public function init() {
$this->view->addScriptPath( APPLICATION_PATH . '/views/scripts/' );
}
Add head.phtml file in view like this:
echo $this->partial('index/head.phtml');
File is successfully added.