here is the code code below
class Home extends Public_Controller
{
/**
* Constructor method
*`enter code here`
* #author PyroCMS Dev Team
* #access public
* #return void
*/
public function __construct()
{
parent::__construct();
}
public function testimg(){
header("Content-type: image/png");
$image = imagecreatetruecolor(200, 200);
imagepng($image);
}
}
but when i call this controller like (http://localhost/sitename/home/testimg).
i got the error below
The image "http://localhost/sitename/home/testimg" cannot be displayed because it contains errors.
Kindly help me with this issue i am new to pyrocms.
Problem Solved : there was always an extra space when echo something, i don't know why - ob_clean() does the job.
public function testimg(){
ob_clean();
header("Content-type: image/png");
$image = imagecreatetruecolor(200, 200);
imagepng($image);
}
That's nothing to do with PyroCMS or even CodeIgniter, you've just set up the image wrong. That is a generic PHP error.
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');
}
Hello im creating i comment system for my laravel app but i get this error when i try to comment.. basically when a user is not logged in and he tries to comment and submit it, it should redirect him to the login page, so my CreateRequest file is this
<?php
namespace App\Http\Requests\Comment;
use App\Http\Requests\Request;
use App\Repositories\Image\ImageRepository;
class CreateRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #param ImageRepository $image
* #return bool
*/
public function authorize(ImageRepository $image) {
$image = $image->getById($this->route('id'))->first();
if (!$image) {
return false;
}
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules() {
return [
'comment' => ['required', 'min:2'],
];
}
/**
* #return \Illuminate\Http\RedirectResponse
*/
public function forbiddenResponse() {
return redirect()->route('login');
}
}
and my App\Http\Requests\Request file is this
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
public function authorize() {
return true;
}
}
if i remove
public function authorize() {
return true;
}
then comments works and if user is not logged in the user gets redirected to the login page but then my login is not working and i get Forbidden when i try to login
im developing my app on top of this project https://github.com/laravelish/EasyAdmin
Hope someone can help me
You've defined an authorize method in your abstract Request class:
public function authorize() {
return true;
}
Note it doesn't take any parameters. Now if you extend this abstract class (like you are doing with CreateRequest) and you write an authorize method, you must use the same signature. Specifically: authorize can't take any parameters, because it doesn't in the abstract class.
If you were to remove the authorize method from your abstract class, this specific error would go away. However I'm not sure that would solve your problem. I don't think Laravel provides dependency injection for the authorize method, you can't just inject your repository like that.
This is one way to fix both issues:
public function authorize() {
$image = app(ImageRepository::class)->getById($this->route('id'))->first();
...
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
In addtion to my previous question Attache zend filters and validation chains to models/doctrine entities I have given a try to Spiffy framework, but I got stack with this exception like this: Exception No form element was specified for "title" and one not be determined automatically from "Spiffy\Zend\Form".
In my entity I have this:
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
use Spiffy\Doctrine\AbstractEntity as Entity;
use Spiffy\Doctrine\Annotations\Filters as Filter;
use Spiffy\Doctrine\Annotations\Validators as Assert;
/** #ORM\Entity(repositoryClass="Repositories\PostRepository") */
class Post extends Entity {
public function __construct()
{
$this->created = new \DateTime("now");
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __get($property)
{
return $this->$property;
}
public function __set($name, $value)
{
$this->$name = $value;
return $this->$name;
}
/**
* #ORM\Id #ORM\Column(type="integer") #ORM\GeneratedValue
*/
private $id;
/**
* #var string $title
* #Filter\Alnum
* #Assert\StringLength(5)
* #ORM\Column(type="string",length=255)
*/
private $title;
/**
* #ORM\Column(type="text")
*/
private $body;
/**
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #ORM\OneToMany(targetEntity="Comment", mappedBy="post", fetch="LAZY")
*/
private $comments;
}
And my form is like this:
<?php
use \Spiffy\Zend\Form as Form;
class Application_Form_Post extends Form
{
public function init()
{
//var_dump($this->getEntity()); //returns null
// die;
$this->add('title');
$this->add('body');
$this->addElement('submit', 'submit', array(
));
}
}
So I am block myself here. Thank you for your help.
In my application.ini, I comented out this lines:
pluginPaths.Bisna\Application\Resource\ = "Bisna/Application/Resource"
and
autoloaderNamespaces[] = Bisna
but I still got the exception:
Uncaught exception 'ReflectionException' with message 'Class Doctrine\ORM\Mapping\Driver\AnnotationDriver does not exist' in C:\Spiffy\lib\Spiffy\Doctrine\Container.php on line 359
What is not clear for me, is that in the bisna resource I had something like this:
\Zend_Registry::set('doctrine', $container);
and in the spiffy resource I had like this:
`Zend_Registry::set('Spiffy_Doctrine', $container);`
But in my Boostrap.php, I had this two:
$this->bootstrap('doctrine');
$container = $this->getResource('doctrine');
I was expected to be a diffrence between doctrine and Spiffy_Doctrine, but is not. And something else that is for me, not understandable. I modify some line in Spiffy container like this:
try{
$reflClass = new ReflectionClass($driverClass);
}catch (LogicException $Exception) {
die('Not gonna make it in here...');
}
catch(ReflectionException $Exception)
{
die('Your class does not exist! ' );
}
but instead of cacthing the exception, I got this:
`Uncaught exception 'ReflectionException'`
Ps: Sorry for the duplication of content from the doctrine group from linekdin, but these are my answers. Rigth now I debug my application, maybe I will figure out what I'm missing, but any help will be great. Thank you.
Can anyone please provide a standard example for developing in Symfony2 using the TDD notation? Or share links to interesting materials for TDD Symfony2 development (except the official documentation :))?
P.S. Is anybody writing unit tests for controller part of MVC pattern?
I just did it for silex, which is a micro-framework based on Symfony2. From what I understand, it's very similar. I'd recommend it for a primer to the Symfony2-world.
I also used TDD to create this application, so what I did was:
I wrote my first test to verify the route/action
Then I implemented the route in my bootstrap
Then I added assertions to my test e.g., what should be displayed
I implemented that in my code and so on
An example testcase (in tests/ExampleTestCase.php) looks like this:
<?php
use Silex\WebTestCase;
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
class ExampleTestCase extends WebTestCase
{
/**
* Necessary to make our application testable.
*
* #return Silex\Application
*/
public function createApplication()
{
return require __DIR__ . '/../bootstrap.php';
}
/**
* Override NativeSessionStorage
*
* #return void
*/
public function setUp()
{
parent::setUp();
$this->app['session.storage'] = $this->app->share(function () {
return new ArraySessionStorage();
});
}
/**
* Test / (home)
*
* #return void
*/
public function testHome()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
}
}
my bootstrap.php:
<?php
require_once __DIR__ . '/vendor/silex.phar';
$app = new Silex\Application();
// load session extensions
$app->register(new Silex\Extension\SessionExtension());
$app->get('/home', function() use ($app) {
return "Hello World";
});
return $app;
My web/index.php:
<?php
$app = require './../bootstrap.php';
$app->run();