removing index.php in ci4 but nothing happen - codeigniter-4

I changed in App.php
public $indexPage = 'index.php';
To
public $indexPage = '';
and
public $uriProtocol = 'PATH_INFO';
and in .htaccess
require realpath(FCPATH . ..'app/Config/Paths.php') ?: FCPATH . ..'/app/Config/Paths.php';

Related

404 - File Not Found Controller or its method is not found: \App\Controllers\Ivy::codeignitor

I have a controller, named About:
<?php
namespace App\Controllers;
class About extends BaseController
{
public function __construct()
{
$this->data['site_title'] = "About Us";
}
public function index()
{
//return view('welcome_message');
return $this->_renderPage('About/index', $this->data);
}
}
the render_page fucntion is in the basecontroller as follows:
public function _renderPage($view, $data = [])
{
$data = array_merge($this->data, $data);
$data['_html_content'] = view('Home/' . $view, $data);
return view('Home/page', $data);
}
My folder structure is:
app
-Controllers
- About
- Home
- Contact
My view files are as follows:
Views
-Home
-Contact
- index.php
-About
- index.php
- ...
The way I reference about is
About
My .htaccess file has this line of code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
Why do I still get the error: 404 - File Not Found
Controller or its method is not found: \App\Controllers\Ivy::codeignitor when I enter url:
localhost:8080/myproject/about
try <?= base_url()?> instead <?= site_url()?>

No logging with configuration made in code using latest version of slf4net (1.0.0)

I'm using the slf4net.log4net nuget package to handle logging in a project. Because it must be possible for the loglevel to change at runtime, I made the configuration in code. The issue is that this code works fine in slf4net.log4net version 0.1.32.1 but when I upgrade it to version 1.0.0, the logfile is created, but the logs are not present on the logfile. I've created a dummy project to show this issue. I do not see how I can add a zip file here, so I'll just post the code here. It is a console app in net framework 4.7.2;
class Program
{
private static string GetLoggingPath()
{
var path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData,
Environment.SpecialFolderOption.DoNotVerify), "LoggingTesting");
Directory.CreateDirectory(path);
return path;
}
static void Main(string[] args)
{
var layout = new PatternLayout
{
ConversionPattern = "%d{ABSOLUTE}: %message %newline"
};
layout.ActivateOptions();
var fileAppender = new RollingFileAppender();
fileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
fileAppender.Layout = layout;
var path = GetLoggingPath();
fileAppender.File = path + System.IO.Path.DirectorySeparatorChar + "LISlogging_.txt";
fileAppender.AppendToFile = true;
fileAppender.PreserveLogFileNameExtension = true;
fileAppender.StaticLogFileName = false;
fileAppender.DatePattern = "yyyy-MM-dd";
fileAppender.MaxSizeRollBackups = 10;
fileAppender.ActivateOptions();
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
BasicConfigurator.Configure(repository, fileAppender);
var root = (repository as Hierarchy)?.Root;
if (root == null) return;
root.Level = log4net.Core.Level.All;
// Create log4net ILoggerFactory and set the resolver
var factory = new slf4net.log4net.Log4netLoggerFactory();
var resolver = new SimpleFactoryResolver(factory);
slf4net.LoggerFactory.SetFactoryResolver(resolver);
// trigger logging
var log = slf4net.LoggerFactory.GetLogger(typeof(Program));
log.Info("log this line");
}
}
public class SimpleFactoryResolver : IFactoryResolver
{
private readonly slf4net.ILoggerFactory _factory;
public SimpleFactoryResolver(slf4net.ILoggerFactory factory)
{
_factory = factory;
}
public slf4net.ILoggerFactory GetFactory()
{
return _factory;
}
}
This dummy project was created in .net framework, but I need this in a .net core project. That is why I need to version 1.0.0 .
I've also post this issue on the github page of slf4net (because it looks like a bug) : https://github.com/ef-labs/slf4net/issues/6
My main question for here on stack overflow is if there is a workaround so this can work with slf4net.log4net version 1.0.0
I've found a workaround for this. Maybe not the cleanest solution but it works. If anyone knows a cleaner solution please add it here.
When looking at the slf4net.log4net code I found out that when it tries to configure log4net it uses xml files or config files, which is a nightmare if you want to set the loglevel at runtime. You can pass a customconfigurator as parameter of the Log4netLoggerFactory . This customconfigurator needs to implement IXmlConfigurator.
The CustomConfigurator I've made accepts an IAppender and a loglevel (log4net.Core.Level). In the implementation of the Configure(ICollection(ILoggerRepository repository) method. I've set the root log level and Configured with the BasicConfigurator.
The CustomConfigurator looks like this:
public class CustomConfigurator: IXmlConfigurator
{
private readonly IAppender _appender;
private readonly log4net.Core.Level _logLevel;
public CustomConfigurator(IAppender appender, log4net.Core.Level logLevel)
{
_appender = appender;
_logLevel = logLevel;
}
public ICollection Configure(ILoggerRepository repository)
{
var root = (repository as Hierarchy)?.Root;
if (root != null)
{
root.Level = _logLevel;
}
return BasicConfigurator.Configure(repository, _appender);
}
public ICollection Configure(ILoggerRepository repository, XmlElement element)
{
return XmlConfigurator.Configure(repository, element);
}
public ICollection Configure(ILoggerRepository repository, FileInfo configFile)
{
return XmlConfigurator.Configure(repository, configFile);
}
public ICollection ConfigureAndWatch(ILoggerRepository repository, FileInfo configFile)
{
return XmlConfigurator.ConfigureAndWatch(repository, configFile);
}
}
Now you can create an appender in code like shown in the question (until fileappender.ActivateOptions) Then when constructing the log4netLoggerFactory you pass an instance of CustomConfigurator which takes the fileAppender and a loglevel as parameter.
var factory = new slf4net.log4net.Log4netLoggerFactory(new CustomConfigurator(fileAppender, Level.All));
var resolver = new SimpleFactoryResolver(factory);
slf4net.LoggerFactory.SetFactoryResolver(resolver);
This should work.

Vanity URLs with IIS URL Rewrite

I feel like this should be easy, but I'm struggling. I'd like a user to be able to go to this url:
http://www.mysite.com/folder/some-id-text
and have URL Rewrite direct that request here:
http://www.mysite.com/folder/index.aspx?id=some-id-text
http://www.mysite.com/folder/some-id-text should be the only url the user ever sees.
in your project edit Global.asax file and add the code below.
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.MapPageRoute("somename",
"folder/{text-id}",
"~/index.aspx");
}
then in your index.aspx you can find this variable as
string text_id = RouteData.Values["text-id"].ToString();
further ref http://code.msdn.microsoft.com/Easy-Steps-to-URL-2f792901

Get absolute path for MSTest Out folder

I have a test with the following attribute:
[DeploymentItem(#"SampleData\do12.vas.pdws.xls")]
When I run the test, my xls file is copied to "TestResults\User Date\Out" folder.
How can I determine the absolute path to this folder from within the test?
I did it like this
[TestMethod]
[DeploymentItem(#"SampleData\do12.vas.pdws.xls")]
public void TestGetCardNumbers()
{
var controller = new MainController();
var controllerContext = new Mock<ControllerContext>();
string assemblyFile = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
string path = System.IO.Path.GetDirectoryName(assemblyFile);
path = Uri.UnescapeDataString(path);
controllerContext.Setup(cc => cc.HttpContext.Session["XlFileUpload"]).Returns(System.IO.Path.Combine(path, "do12.vas.pdws.xls"));

SharePoint 2010 folder woes

I've put together a function that creates a sharepoint folder in a document library based on the url that's past in as an argument. The code works and the folder shows up in sharepoint from the webapplication.
However, when I query the SPWeb object for the folder afterward, it says the folder doesnt exist. Which makes no sense to me. Stranger still, is that this very same code worked no too long ago. I had been using it to create tree structures in sharepoint.
Even if the query folder fails, the GetFolder still returns a the folder, but when I add files to the returned folder, I get a runtime exception indicating that the file doesn't exist...which I assume means the folder I am trying to add it to doesn't exist since the file I am adding, doesn't exist yet. Which is why I am adding it.
So my question is, why am I getting this error, and why does FolderExists return false when the folder actually exists? We know it exists because GetFolder actually returns it...
I've included some actual code from the app to make things clear.
If someone could have a look at the code and see and anything jumps out at them, that would be fantabulous...Thanks
Code to build folders:
public void CreateFolder(SPUriBuilder url)
{
try
{
Log.Instance.WriteToLog("CreateFolder({0})", url);
var library = GetLibrary(url.Library);
if (library != null)
{
// parse out string data
//
var parent = library.RootFolder.ServerRelativeUrl;
var segments = url.Account.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var path = parent;
// get default folder collection
//
SPFolderCollection subFolders = _web.GetFolder(parent).SubFolders;
// check for sub-folders to create
//
if (segments.Length > 0)
{
int i = 0;
do
{
// check for folder and create if non-existant
//
var buildPath = String.Format("{0}/{1}", path, segments[i]);
if (_web.GetFolder(buildPath).Exists == false)
_web.GetFolder(path).SubFolders.Add(segments[i]);
// retrieve new sub-folder collection
//
subFolders = _web.GetFolder(buildPath).SubFolders;
path = buildPath;
// next folder in path
//
i++;
}
while (i < segments.Length);
}
// finally, add folder of interest
//
subFolders.Add(url.Folder);
}
}
catch (Exception e)
{
throw new SPImportException("Exception: {0}, creating folder: {1} in Library: {2}", e.Message, url.Folder, url.Library);
}
}
Code to Query folder:
public bool FolderExists(SPUriBuilder url)
{
return _web.GetFolder(url.Uri.LocalPath).Exists;
}
Code to Get Folder:
private SPFolder GetFolder(SPUriBuilder url)
{
return _web.GetFolder(url.Uri.LocalPath);
}
The SPUriBuilder is a custom class I created to assemble the Uri:
public class SPUriBuilder
{
public string SiteUrl { get; private set; }
public string Library { get; private set; }
public string Parent { get; private set; }
public string Folder { get; private set; }
public string File { get; private set; }
public string Account { get; private set; }
public Uri Uri { get; private set; }
public SPUriBuilder(string siteUrl, string library, string account, string parent, string folder)
{
this.SiteUrl = siteUrl;
this.Library = library;
this.Account = account;
this.Parent = parent.Replace("\\", "/");
this.Parent = this.Parent.StartsWith("/") ? this.Parent.Substring(1) : this.Parent;
this.Folder = folder;
StringBuilder url = new StringBuilder();
url.AppendFormat("{0}/{1}/{2}", SiteUrl, Library, Account);
if (String.IsNullOrEmpty(Parent) == false)
url.AppendFormat("/{0}", Parent);
url.AppendFormat("/{0}", Folder);
this.Uri = new Uri(url.ToString());
}
public SPUriBuilder(SPUriBuilder uri, string file)
: this(uri.SiteUrl, uri.Library, uri.Account, uri.Parent, uri.Folder)
{
this.File = file;
StringBuilder url = new StringBuilder();
url.AppendFormat("{0}/{1}", this.Uri.ToString(), this.File);
this.Uri = new Uri(url.ToString());
}
public override string ToString()
{
return Uri.ToString();
}
}
I found the answer this to this myself. The problem was in the code used to create the folder.
var parent = library.RootFolder.ServerRelativeUrl;
// This line of code is incorrect, so it returned the wrong data, thus building the folder path incorrectly.
//
var segments = url.Account.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var path = parent;
// This is the replacement line of code that fixed the issue.
//
var segments = url.Uri.LocalPath.Substring(parent.Length+1).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
// as well, this line had to be removed since it was no longer needed
//
// finally, add folder of interest
//
subFolders.Add(url.Folder);
Ultimately the issue turned out be that the folder structure did not exist that I was attempting to create the file in. One or more segments in the path were missing.
So if you ever see this error, make sure you're the folder exists that you are adding the file to. If it isn't, you will certainly experience this error.

Resources